No.5
2020-04-15
【题目】cf915D Almost Acyclic Graph
【思路】
省略……
【代码】
#include<bits/stdc++.h>
using namespace std;
inline int read(int &a){a=0;int f=1;char c=getchar();if(c==EOF) return a=EOF;
for(;c<'0' || c>'9';c=getchar()){if(c=='-') f=-1;if(c==EOF) return a=EOF;}
for(;c<='9' && c>='0';a=a*10+c-'0',c=getchar());return a*=f;}
inline void print(int a){int s[20],t=0;if(!a){putchar('0');putchar('\n');return;}
if(a<0) a=-a,putchar('-');while(a){s[t++]=a%10,a/=10;}
for(;t--;putchar(s[t]+'0'));putchar('\n');}
int n,m;
vector<int> p[510];
int in[510];
queue<int> q;
bool pd(int n)
{
int t[510],s=0;
memcpy(t,in,sizeof(t));
while(!q.empty()) q.pop();
for(int i=1;i<=n;i++)
if(!t[i]) q.push(i),s++;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0;i<p[x].size();i++)
{
int y=p[x][i];
t[y]--;
if(!t[y]) q.push(y),s++;
}
}
return s>=n;
}
int main()
{
freopen("graph.in","r",stdin);
freopen("graph.out","w",stdout);
read(n),read(m);
for(int i=1;i<=m;i++)
{
int u,v;
read(u),read(v);
in[v]++;
p[u].push_back(v);
}
for(int i=1;i<=n;i++)
{
if(in[i])
{
in[i]--;
if(pd(n))
{
puts("YES");
return 0;
}
in[i]++;
}
}
puts("NO");
return 0;
}