HDU 4324 Triangle LOVE【拓扑排序】

题意:给出n个人,如果a喜欢b,那么b一定不喜欢a,如果b不喜欢a,那么a一定喜欢b

就是这n个点里面的任意两点都存在一条单向的边, 所以如果这n个点不能构成拓扑序列的话,就一定成环了,成环的话就一定能够找到一个三元环

所以只需要判断能不能构成拓扑序列

 

另外,tle了一晚上是因为用了cin------55555555555555

以后少用cin了-----

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath> 
 5 #include<stack>
 6 #include<vector>
 7 #include<map> 
 8 #include<set>
 9 #include<queue> 
10 #include<algorithm>  
11 using namespace std;
12 
13 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
14 
15 typedef long long LL;
16 const int INF = (1<<30)-1;
17 const int mod=1000000007;
18 const int maxn=2005;
19 
20 vector<int> g[maxn];
21 int in[maxn],ans[maxn];
22 char str[maxn];
23 int n;
24 
25 void toposort(){
26     queue<int> q;
27     for(int i=0;i<n;i++)
28     if(in[i]==0) q.push(i);
29     
30     int cnt=0;
31     while(!q.empty()){
32         int x=q.front();q.pop();
33         ans[++cnt]=x;
34         
35         for(int j=0;j<g[x].size();j++){
36             int m=g[x][j];
37             in[m]--;
38             if(in[m]==0) q.push(m);            
39         }
40     }
41     if(cnt==n) printf("No\n");
42     else printf("Yes\n");
43 }
44 
45 
46 
47 
48 int main(){
49     int T;
50     cin>>T;
51     for(int t=1;t<=T;t++){
52         cin>>n;
53         
54         memset(in,0,sizeof(in));
55         
56         for(int i=0;i<n;i++){
57             g[i].clear();
58             scanf("%s",str);
59             
60             for(int j=0;j<n;j++){
61                 if(str[j]=='1') g[i].push_back(j),in[j]++;
62             }
63         }
64         printf("Case #%d: ",t);
65         toposort();    
66     }
67     return 0;
68 }
View Code

 

 

 

 

 

 

 

 

 

 

现在想想,觉得这道题目好悲伤啊-------------- 唉

转载于:https://www.cnblogs.com/wuyuewoniu/p/4456583.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值