Briefly meaning of the questions:
Began to seek from each point, the maximum number of points that can be reached.
We just found a property, this problem becomes quite simple.
You want, if you start to go from each point, each traverse, certainly unscientific.
Because it is a directed graph, so the current point maximum number that can be reached , we reverse the construction drawing, must also be able to go . Moreover, they are able to come point, after reverse FIG construction, can come to them; if you can not come point, the reverse construction Figure, can not come to them.
Therefore, we reverse the construction drawing, descending to traverse.
time complexity: .
Actual score: .
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1;
inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}
int n,m; int h[N]; //每个点的答案
vector<int>G[N];
inline void dfs(int dep,int top) {
if(h[dep]) return;
h[dep]=top; //记录答案的同时做哈希,因为先遍历到的答案肯定比后遍历的答案优
for(int i=0;i<G[dep].size();i++)
dfs(G[dep][i],top);
}
int main(){
n=read(),m=read(); while(m--) {
int x=read(),y=read();
G[y].push_back(x);
} for(int i=n;i>=1;i--)
if(!h[i]) dfs(i,i);
for(int i=1;i<=n;i++) printf("%d ",h[i]);
return 0;
}