题目很好理解,解法就是按照压的关系连有向边,拓扑排序即可。亏我一开始还打了10分钟缩点。
#include<iostream> #include<cstdio> #include<algorithm> #include<queue> using namespace std; const int maxn=1e6+50; int n,m,tot,ans; int head[maxn],ru[maxn]; struct EDGE { int to;int next; }edge[maxn]; inline int read() { char ch=getchar(); int s=0,f=1; while (!(ch>='0'&&ch<='9')) {if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9') {s=(s<<3)+(s<<1)+ch-'0';ch=getchar();} return s*f; } void add(int x,int y) { edge[++tot]=(EDGE){y,head[x]}; head[x]=tot; } void topo() { queue <int> q; for (int i=1;i<=n;i++) if (!ru[i]) {q.push(i);ans++;} while (!q.empty()) { int k=q.front();q.pop(); for (int i=head[k];i;i=edge[i].next) { int y=edge[i].to; ru[y]--; if (!ru[y]) { ans++; q.push(y); } } } } int main() { freopen("mikado.in","r",stdin); freopen("mikado.out","w",stdout); n=read();m=read(); for (int i=1;i<=m;i++) { int x=read(),y=read(); add(x,y); ru[y]++; } topo(); printf("%d ",ans); return 0; }