zoukankan      html  css  js  c++  java
  • 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
    ");
    42     else printf("Yes
    ");
    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

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

  • 相关阅读:
    颠覆想象的php解析获取跨域HTML标签
    Win7承载网络配置——让你的手机无线上网吧
    Joomla!备忘手记
    jQuery+PHP+MySQL简单无限级联实现
    js 功能函数集
    PHP POST数据至远程服务器获取信息
    js生成迅雷地址
    强大的jQuery选择器之选定连续多行
    札记 PHP/JS/jQuery/MySQL/CSS/正则/Apache
    php数组一对一替换
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4456583.html
Copyright © 2011-2022 走看看