题干:

We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being called edges. Then G=(V,E)is called a directed graph. 
Let n be a positive integer, and let p=(e1,...,en) be a sequence of length n of edges ei∈E such that ei=(vi,vi+1) for a sequence of vertices (v1,...,vn+1). Then p is called a path from vertex v1 to vertex vn+1 in G and we say that vn+1 is reachable from v1, writing (v1→vn+1)
Here are some new definitions. A node v in a graph G=(V,E) is called a sink, if for every node w in G that is reachable from vv is also reachable from w. The bottom of a graph is the subset of all nodes that are sinks, i.e., bottom(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}. You have to calculate the bottom of certain graphs.

Input

The input contains several test cases, each of which corresponds to a directed graphG. Each test case starts with an integer number v, denoting the number of vertices of G=(V,E), where the vertices will be identified by the integer numbers in the setV={1,...,v}. You may assume that 1<=v<=5000. That is followed by a non-negative integer e and, thereafter, e pairs of vertex identifiers v1,w1,...,ve,we with the meaning that (vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.

Output

For each test case output the bottom of the specified graph on a single line. To this end, print the numbers of all nodes that are sinks in sorted order separated by a single space character. If the bottom is empty, print an empty line.

Sample Input

3 3
1 3 2 3 3 1
2 1
1 2
0

Sample Output

1 3
2

题目大意:

定义点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径;若v不可以到达u,则u到v的路径可有可无。问在n个点m条边的有向图里面,问有多少个点是汇点。

解题报告:

缩点后看出度为0的点的个数就行了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
vector<int> vv[MAX],ans;
int n,m;
int dfn[MAX],low[MAX],vis[MAX],out[MAX],cnt[MAX],col[MAX];
int stk[MAX],index,clk,scc;
void init() {
for(int i = 1; i<=n; i++) {
vv[i].clear();
dfn[i]=low[i]=vis[i]=out[i]=cnt[i]=0;
}
clk = index = scc = 0;
ans.clear();
}
void tarjan(int x) {
stk[++index] = x;
vis[x] = 1;
dfn[x] = low[x] = ++clk;
int up = vv[x].size();
for(int i = 0; i<up; i++) {
int v = vv[x][i];
if(dfn[v] == 0) {
tarjan(v);
low[x] = min(low[x],low[v]);
}
else if(vis[v] == 1) low[x] = min(low[x],dfn[v]);
}
if(dfn[x] == low[x]) {
scc++;
while(1) {
int tmp = stk[index];index--;
col[tmp] = scc;
cnt[scc]++;
vis[tmp]=0;
if(tmp == x) break;
}
}
}
int main()
{
while(~scanf("%d",&n)) {
if(n == 0) break;
scanf("%d",&m);
init();
for(int u,v,i = 1; i<=m; i++) {
scanf("%d%d",&u,&v);
vv[u].pb(v);
}
for(int i = 1; i<=n; i++) {
if(dfn[i] == 0) tarjan(i);
}
for(int up,u = 1; u<=n; u++) {
up = vv[u].size();
for(int v,j = 0; j<up; j++) {
v = vv[u][j];
if(col[u] == col[v]) continue;
out[col[u]]++;
}
}
for(int i = 1; i<=n; i++) {
if(out[col[i]] == 0) ans.pb(i);
}
sort(ans.begin(),ans.end());
int up = ans.size();
for(int i = 0; i<up; i++) printf("%d%c",ans[i],i == up-1 ? '\n' : ' ');
}
return 0 ;
}