标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10908 Accepted Submission(s): 4887
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<string> #include<map> #include<stack> #include<set> #include<iostream> #include<vector> #include<queue> //ios_base::sync_with_stdio(false); //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define repf(i, a, b) for (int i = (a); i <= (b); ++i) #define repd(i, a, b) for (int i = (a); i >= (b); --i) #define clr(x) memset(x,0,sizeof(x)) #define sqr(x) ((x) * (x)) typedef long long LL; const int INF = 1000000000; const double eps = 1e-8; const int maxn = 10000 + 10; const int mod = 1000000000 + 7; int vis[maxn]; int left_time[maxn]; int n,m; vector<int> g[maxn]; int max_time; void dfs(int n){ if(vis[n]) return; vis[n] = 1; rep(i,0,g[n].size()){ if(vis[g[n][i]] == 0){ dfs(g[n][i]); } } left_time[++max_time] = n; return ; } void dfs_ans(int n){ if(vis[n]) return ; vis[n] = 1; rep(i,0,g[n].size()){ if(!vis[g[n][i]]){ dfs_ans(g[n][i]); } } return ; } int ans(){ int ret = 0; clr(vis); repf(i,1,max_time){ if(vis[left_time[i]] == 0){ ret++; if(ret > 1) return 1; dfs_ans(left_time[i]); } } return 0; } int main() { //freopen("in.txt","r",stdin); while(scanf("%d%d",&n,&m) == 2){ if(n == 0 && m == 0) break; max_time = 0; clr(vis); clr(left_time); repf(i,1,n) g[i].clear(); rep(i,0,m){ int a,b; scanf("%d%d",&a,&b); g[a].push_back(b); } dfs(1); int flag = 0; repf(i,1,n) if(vis[i] == 0) flag = 1; if(flag || ans()) printf("No\n"); else printf("Yes\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/NothingElsePKU/p/5052497.html