zoukankan      html  css  js  c++  java
  • bzoj1116: [POI2008]CLO

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1116

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

    题解:并查集

    代码:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cstdio>
     5 #include<cmath>
     6 #define maxn 100005
     7 using namespace std;
     8 int n,m;
     9 int fa[maxn];
    10 bool mark[maxn];
    11 int read()
    12 {
    13     int x=0; char ch; bool bo=0;
    14     while (ch=getchar(),ch<'0'||ch>'9') if (ch=='-') bo=1;
    15     while (x=x*10+ch-'0',ch=getchar(),ch>='0'&&ch<='9') ;
    16     if (bo) return -x; return x;
    17 }
    18 int find(int x)
    19 {
    20     if (fa[x]!=x) fa[x]=find(fa[x]);
    21     return fa[x];
    22 }
    23 int main()
    24 {
    25     n=read(); m=read();
    26     for (int i=1; i<=n; i++) fa[i]=i;
    27     for (int i=1; i<=m; i++)
    28     {
    29         int u=read(),v=read();
    30         int q=find(u),p=find(v);
    31         if (q!=p)
    32         {
    33             fa[q]=p; mark[p]=mark[p] | mark[q];
    34         }
    35         else
    36         mark[q]=1;
    37     }
    38     for (int i=1; i<=n; i++) 
    39     if (!mark[find(i)]) 
    40     {
    41         printf("NIE");
    42         return 0;
    43     }
    44     printf("TAK"); return 0;
    45 }
    View Code
  • 相关阅读:
    Pandas数据存取
    Pandas统计分析
    (4)awk读取行的细节
    (3)awk命令行结构和语法结构
    安装新版gawk
    (2)BEGIN和END语句块
    (1)AWK快速入门
    shell读取文件的几种方式
    Docker 部署 elk + filebeat
    Linux之关于用户的几个重要配置文件
  • 原文地址:https://www.cnblogs.com/HQHQ/p/5578220.html
Copyright © 2011-2022 走看看