• (并查集) bzoj 1116


    Description

    Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 你要把其中一些road变成单向边使得:每个town都有且只有一个入度

    Input

    第一行输入n m.1 <= n<= 100000,1 <= m <= 200000 下面M行用于描述M条边.

    Output

    TAK或者NIE 常做POI的同学,应该知道这两个单词的了...

    Sample Input

    4 5
    1 2
    2 3
    1 3
    3 4
    1 4


    Sample Output

    TAK

    上图给出了一种连接方式.
    问题判断判断每个联通块是否有环就好,把根节点标记下。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<string>
    using namespace std;
    #define maxn 100010
    int n,m,set[maxn];
    bool mark[maxn];
    int find(int x)
    {
          if(x!=set[x])
                set[x]=find(set[x]);
          return set[x];
    }
    int main()
    {
          int x,y;
          memset(mark,0,sizeof(mark));
          scanf("%d%d",&n,&m);
          for(int i=1;i<=n;i++)
                set[i]=i;
          for(int i=0;i<m;i++)
          {
              scanf("%d%d",&x,&y);
              int f1,f2;
              f1=find(x),f2=find(y);
              if(f1==f2)
                      mark[f1]=1;
              else
              {
                      set[f1]=f2;
                      mark[f2]=mark[f1]|mark[f2];
              }
          }
          for(int i=1;i<=n;i++)
                if(!mark[set[i]])
                {
                      printf("NIE
    ");
                      return 0;
                }
          printf("TAK
    ");
          return 0;
    }
    

      

  • 相关阅读:
    shell基础
    函数属性
    this的使用
    循环
    正则表达式中的方法
    判断是不是数组
    ECMAScript5中数组方法
    ECMAScript3中数组方法
    break和continue、return的区别
    用来枚举属性的对象工具函数
  • 原文地址:https://www.cnblogs.com/a972290869/p/4228041.html
Copyright © 2020-2023  润新知