标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16486 | Accepted: 4386 |
Description
Input
Output
Sample Input
1 3 3 1 2 2 3 3 1
Sample Output
Yes
/* * Author: sweat123 * Created Time: 2016/6/25 12:33:09 * File Name: main.cpp */ #include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<string> #include<vector> #include<cstdio> #include<time.h> #include<cstring> #include<iostream> #include<algorithm> #define INF 1<<30 #define MOD 1000000007 #define ll long long #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define pi acos(-1.0) using namespace std; const int MAXN = 20000; struct node{ int from; int to; int next; }edge[MAXN*1000]; int pre[MAXN],vis[MAXN],dfn[MAXN],low[MAXN],n,m,ind; int f[MAXN],num,k; stack<int>s; void add(int x,int y){ edge[ind].from = x; edge[ind].to = y; edge[ind].next = pre[x]; pre[x] = ind ++; } void dfs(int rt){ vis[rt] = 1; dfn[rt] = low[rt] = ++k; s.push(rt); for(int i = pre[rt]; i != -1; i = edge[i].next){ int t = edge[i].to; if(!dfn[t]){ dfs(t); low[rt] = min(low[rt],low[t]); } else if(vis[t]){ low[rt] = min(low[rt],dfn[t]); } } if(low[rt] == dfn[rt]){ ++ num; while(!s.empty()){ int tp = s.top(); s.pop(); vis[tp] = 0; f[tp] = num; if(tp == rt)break; } } } int x[MAXN],y[MAXN],ans,in[MAXN]; int ok(){ queue<int>q; for(int i = 1; i <= num; i++){ if(in[i] == 0){ q.push(i); } } if(q.size() > 1)return 0; while(!q.empty()){ int tp = q.front(); q.pop(); for(int i = pre[tp]; i != -1; i = edge[i].next){ int t = edge[i].to; in[t] --; if(in[t] == 0){ q.push(t); } } if(q.size() > 1)return 0; } return 1; } int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); ind = 0; while(!s.empty())s.pop(); memset(pre,-1,sizeof(pre)); for(int i = 1; i <= m; i++){ scanf("%d%d",&x[i],&y[i]); add(x[i],y[i]); } k = 0; num = 0; memset(vis,0,sizeof(vis)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(f,0,sizeof(f)); for(int i = 1; i <= n; i++){ if(!dfn[i])dfs(i); } memset(pre,-1,sizeof(pre)); memset(in,0,sizeof(in)); int ret = ind; for(int i = 0; i < ret; i++){ int u = f[edge[i].from]; int v = f[edge[i].to]; if(u != v){ add(u,v); in[v] ++; } } if(ok()){ printf("Yes\n"); } else{ printf("No\n"); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/sweat123/p/5616164.html