zoukankan      html  css  js  c++  java
  • MUTC 3 E Triangle LOVE 图论/搜索

    Triangle LOVE

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1629    Accepted Submission(s): 693


    Problem Description
    Recently, scientists find that there is love between any of two people. For example, between A and B, if A don’t love B, then B must love A, vice versa. And there is no possibility that two people love each other, what a crazy world!
    Now, scientists want to know whether or not there is a “Triangle Love” among N people. “Triangle Love” means that among any three people (A,B and C) , A loves B, B loves C and C loves A.
      Your problem is writing a program to read the relationship among N people firstly, and return whether or not there is a “Triangle Love”.
     

    Input
    The first line contains a single integer t (1 <= t <= 15), the number of test cases.
    For each case, the first line contains one integer N (0 < N <= 2000).
    In the next N lines contain the adjacency matrix A of the relationship (without spaces). Ai,j = 1 means i-th people loves j-th people, otherwise Ai,j = 0.
    It is guaranteed that the given relationship is a tournament, that is, Ai,i= 0, Ai,j ≠ Aj,i(1<=i, j<=n,i≠j).
     

    Output
    For each case, output the case number as shown and then print “Yes”, if there is a “Triangle Love” among these N people, otherwise print “No”.
    Take the sample output for more details.
     

    Sample Input
    2 5 00100 10000 01001 11101 11000 5 01111 00000 01000 01100 01110
     

    Sample Output
    Case #1: Yes Case #2: No
     

    Author
    BJTU
     

    Source
     

    Recommend
    zhoujiaqi2010

    ----------------

    图论做法:

    一个结论:竞赛图上只要有环,就有三元环。

    搜索做法:

    带上父节点直接搜= =

    标准做法1:

    	增量算法,充分利用“任意两点间仅有一条有向边”的性质。
    	假设前面已经加入了N个点,现在来了第N+1个点。
    	那么一定能将N个点分成left和right两部分,使得N+1号点到left有边,right到N+1号点右边(因为任意两点间都有边),那么,如果left的任意一个点l到right任意一个点r有边的话,那么就有答案N+1->l->r->N+1这样一个长度为3的环。
    	那么每次加入N+1号点后,用O(N)的复杂度求出左边的数量leftnum,右边的数量rightnum,left的出度和leftout,left的入度和leftin。
    	如果left没有一条到right的边,则一定满足:
    	leftin = leftout + leftnum * rightnum(left和right任意两点右边,如果没有左到右的,那么leftnum*rightnum条边都是右到左的)
    	那么,如果leftin != leftout + leftnum * rightnum,则暴力枚举左点,右点即可得到答案。
    	总体复杂度O(n^2)

    标准做法2:

    	归结为1个条件,任何两人都有边,要么出要么入。
    	对于有向三元环,我们知道找到:A->B->C->A ,B->C->A->B ,C->A->B->C 是一样的,这给0(n^2)的算法提供了基础。
            对于每次枚举i,我们在(0~i-1)的范围看下有多少个i指向的点(剩下的就是指向i的点),同时算下i指向的点的出度和。就可以知道这些 i指向的点 指向 指向i的点(剩下的点)的数目,如果 
            num * (num - 1) / 2 < sumout 那么就是被指向的点有指出去了,这样就形成了3元环。
            否则,剩下的就是更新下出度即可,继续执行下一个节点。
     

    ------------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    const int maxn=2222;
    
    char s[maxn][maxn];
    
    int inq[maxn];
    int n;
    
    bool top_sort()
    {
        int num=n;
        queue<int>que;
        for (int i=0;i<n;i++)
            if (!inq[i]) {que.push(i);num--;}
        while (!que.empty())
        {
            int top=que.front();
            que.pop();
            for (int i=0;i<n;i++)
            {
                if (top!=i&&s[top][i]=='1')
                {
                    inq[i]--;
                    if (!inq[i]) {que.push(i);num--;}
                }
            }
        }
        if (num) return true;
        else return false;
    
    }
    
    int main()
    {
        int T,cnt=0;;
        scanf("%d",&T);
        while (T--)
        {
            memset(inq,0,sizeof(inq));
            scanf("%d",&n);
            for (int i=0;i<n;i++)
            {
                scanf("%s",s[i]);
            }
            for (int i=0;i<n;i++)
                for (int j=0;j<n;j++)
                    if (i!=j&&s[i][j]=='1') inq[j]++;
            bool ret=top_sort();
            printf("Case #%d: ",++cnt);
            if (ret) puts("Yes");
            else puts("No");
        }
        return 0;
    }

    ---------------

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    bool v[5555]= {0};
    int n;
    char s[5555][5555];
    bool dfs(int i,int dad)
    {
        v[i]=true;
        for (int j=1; j<=n; j++)
            if (s[i][j]-'0')
            {
                if (s[j][dad]-'0') return true;
                if (!v[j]) if (dfs(j,i)) return true;
            }
        return false;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        for (int lp=1; lp<=T; lp++)
        {
            memset(v,0,sizeof(v));
            scanf("%d",&n);
            for (int i=1; i<=n; i++)
            {
                scanf("%s",s[i]+1);
            }
            printf("Case #%d: ",lp);
            bool flag=false;
            for (int i=1; i<=n; i++)
            {
                if (!v[i])
                {
                    if (dfs(i,i)){flag=true;break;}
                }
            }
            if (!flag) printf("No\n");
            else printf("Yes\n");
        }
        return 0;
    }
    






  • 相关阅读:
    Windows Phone开发(29):隔离存储C 转:http://blog.csdn.net/tcjiaan/article/details/7447469
    Windows Phone开发(25):启动器与选择器之WebBrowserTask 转:http://blog.csdn.net/tcjiaan/article/details/7404770
    内存知识集
    牛人榜
    如何解决SQL Server 2000 中的连接问题(邹建)
    索引
    .net事件机制
    内核对象
    使用socket tcp实现通讯
    sql技巧
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226301.html
Copyright © 2011-2022 走看看