{"trustable":false,"prependHtml":"\u003cstyle type\u003d\"text/css\"\u003e\n section pre {\n display: block;\n padding: 9.5px;\n margin: 0 0 10px;\n font-size: 13px;\n line-height: 1.42857143;\n word-break: break-all;\n word-wrap: break-word;\n color: #333;\n background: rgba(255, 255, 255, 0.5);\n border: 1px solid #ccc;\n border-radius: 6px;\n }\n\u003c/style\u003e\n\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027\\\\(\u0027, right: \u0027\\\\)\u0027, display: false},\n {left: \u0027\\\\[\u0027, right: \u0027\\\\]\u0027, display: true}\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"Problem Statement","value":{"format":"MD","content":"## 题目描述\n\n给定 $N$ 个点 $M$ 条边的简单有向图 $G$,点编号为 $1 \\dots N$。第 $i$ 条边($1 \\le i \\le M$)从点 $U_i$ 连到 $V_i$。\n\nTakahashi 选择一个点作为起点,然后沿着 $G$ 上的有向边不断走下去。有多少个点满足:Takahashi 选择在这个结点出发,如果他刻意谨慎地选择某些边走,他可以无限地走下去而不停止。\n\n## 数据范围\n\n* $1 \\le N \\le 2 \\times 10^5$\n* $0 \\le M \\le \\min(N(N - 1), 2 \\times 10^5)$\n* $1 \\le U_i, V_i \\le N$\n* $U_i \\ne V_i$\n* $(U_i, V_i) \\ne (U_j, V_j)(i \\ne j)$\n* 所有输入均为整数。\n\n\u003csection\u003e\n \u003cp\u003eWe have a simple directed graph \u003cvar\u003e\\(G\\)\u003c/var\u003e with \u003cvar\u003e\\(N\\)\u003c/var\u003e vertices and \u003cvar\u003e\\(M\\)\u003c/var\u003e edges. The vertices are labeled as Vertex \u003cvar\u003e\\(1\\)\u003c/var\u003e, Vertex \u003cvar\u003e\\(2\\)\u003c/var\u003e, \u003cvar\u003e\\(\\ldots\\)\u003c/var\u003e, Vertex \u003cvar\u003e\\(N\\)\u003c/var\u003e. The \u003cvar\u003e\\(i\\)\u003c/var\u003e-th edge \u003cvar\u003e\\((1\\leq i\\leq M)\\)\u003c/var\u003e goes from Vertex \u003cvar\u003e\\(U_i\\)\u003c/var\u003e to Vertex \u003cvar\u003e\\(V_i\\)\u003c/var\u003e.\u003c/p\u003e\n \u003cp\u003eTakahashi will start at a vertex and repeatedly travel on \u003cvar\u003e\\(G\\)\u003c/var\u003e from one vertex to another along a directed edge. How many vertices of \u003cvar\u003e\\(G\\)\u003c/var\u003e have the following condition: Takahashi can start at that vertex and continue traveling indefinitely by carefully choosing the path?\u003c/p\u003e\n\u003c/section\u003e"}},{"title":"Constraints","value":{"format":"MD","content":"\u003csection\u003e\n \u003cul\u003e\n \u003cli\u003e\u003cvar\u003e\\(1 \\leq N \\leq 2\\times 10^5\\)\u003c/var\u003e\u003c/li\u003e\n \u003cli\u003e\u003cvar\u003e\\(0 \\leq M \\leq \\min(N(N-1), 2\\times 10^5)\\)\u003c/var\u003e\u003c/li\u003e\n \u003cli\u003e\u003cvar\u003e\\(1 \\leq U_i,V_i\\leq N\\)\u003c/var\u003e\u003c/li\u003e\n \u003cli\u003e\u003cvar\u003e\\(U_i\\neq V_i\\)\u003c/var\u003e\u003c/li\u003e\n \u003cli\u003e\u003cvar\u003e\\((U_i,V_i)\\neq (U_j,V_j)\\)\u003c/var\u003e if \u003cvar\u003e\\(i\\neq j\\)\u003c/var\u003e.\u003c/li\u003e\n \u003cli\u003eAll values in input are integers.\u003c/li\u003e\n \u003c/ul\u003e\n\u003c/section\u003e"}},{"title":"Input","value":{"format":"MD","content":"\u003csection\u003e\n \u003cp\u003eInput is given from Standard Input in the following format:\u003c/p\u003e\n \u003cpre\u003e\u003cvar\u003e\\(N\\)\u003c/var\u003e \u003cvar\u003e\\(M\\)\u003c/var\u003e\n\u003cvar\u003e\\(U_1\\)\u003c/var\u003e \u003cvar\u003e\\(V_1\\)\u003c/var\u003e\n\u003cvar\u003e\\(U_2\\)\u003c/var\u003e \u003cvar\u003e\\(V_2\\)\u003c/var\u003e\n\u003cvar\u003e\\(\\vdots\\)\u003c/var\u003e\n\u003cvar\u003e\\(U_M\\)\u003c/var\u003e \u003cvar\u003e\\(V_M\\)\u003c/var\u003e\n\u003c/pre\u003e\n\u003c/section\u003e"}},{"title":"Output","value":{"format":"MD","content":"\u003csection\u003e\n \u003cp\u003ePrint the answer.\u003c/p\u003e\n\u003c/section\u003e"}},{"title":"Sample 1","value":{"format":"MD","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e5 5\n1 2\n2 3\n3 4\n4 2\n4 5\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e4\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003csection\u003e\n\u003c/section\u003e\u003csection\u003e\n \u003cp\u003eWhen starting at Vertex \u003cvar\u003e\\(2\\)\u003c/var\u003e, Takahashi can continue traveling indefinitely: \u003cvar\u003e\\(2\\)\u003c/var\u003e \u003cvar\u003e\\(\\to\\)\u003c/var\u003e \u003cvar\u003e\\(3\\)\u003c/var\u003e \u003cvar\u003e\\(\\to\\)\u003c/var\u003e \u003cvar\u003e\\(4\\)\u003c/var\u003e \u003cvar\u003e\\(\\to\\)\u003c/var\u003e \u003cvar\u003e\\(2\\)\u003c/var\u003e \u003cvar\u003e\\(\\to\\)\u003c/var\u003e \u003cvar\u003e\\(3\\)\u003c/var\u003e \u003cvar\u003e\\(\\to\\)\u003c/var\u003e \u003cvar\u003e\\(\\cdots\\)\u003c/var\u003e The same goes when starting at Vertex \u003cvar\u003e\\(3\\)\u003c/var\u003e or Vertex \u003cvar\u003e\\(4\\)\u003c/var\u003e. From Vertex \u003cvar\u003e\\(1\\)\u003c/var\u003e, he can first go to Vertex \u003cvar\u003e\\(2\\)\u003c/var\u003e and then continue traveling indefinitely again.\u003cbr\u003e\n On the other hand, from Vertex \u003cvar\u003e\\(5\\)\u003c/var\u003e, he cannot move at all.\u003c/p\u003e\n \u003cp\u003eThus, four vertices ―Vertex \u003cvar\u003e\\(1\\)\u003c/var\u003e, \u003cvar\u003e\\(2\\)\u003c/var\u003e, \u003cvar\u003e\\(3\\)\u003c/var\u003e, and \u003cvar\u003e\\(4\\)\u003c/var\u003e― satisfy the conditions, so \u003cvar\u003e\\(4\\)\u003c/var\u003e should be printed.\u003c/p\u003e\n\u003c/section\u003e"}},{"title":"Sample 2","value":{"format":"MD","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3 2\n1 2\n2 1\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e2\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003csection\u003e\n\u003c/section\u003e\u003csection\u003e\n \u003cp\u003eNote that, in a simple directed graph, there may be two edges in opposite directions between the same pair of vertices. Additionally, \u003cvar\u003e\\(G\\)\u003c/var\u003e may not be connected.\u003c/p\u003e\n\u003c/section\u003e"}}]}