zoukankan      html  css  js  c++  java
  • hdu6370 并查集+dfs

    Werewolf

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 2363    Accepted Submission(s): 713


    Problem Description
    "The Werewolves" is a popular card game among young people.In the basic game, there are 2 different groups: the werewolves and the villagers.

    Each player will debate a player they think is a werewolf or not. 

    Their words are like "Player x is a werewolf." or "Player x is a villager.".

    What we know is :

    1. Villager won't lie.

    2. Werewolf may lie. 

    Of cause we only consider those situations which obey the two rules above. 

    It is guaranteed that input data exist at least one situation which obey the two rules above.

    Now we can judge every player into 3 types :

    1. A player which can only be villager among all situations, 

    2. A player which can only be werewolf among all situations.

    3. A player which can be villager among some situations, while can be werewolf in others situations.

    You just need to print out the number of type-1 players and the number of type-2 players. 

    No player will talk about himself.
     
    Input
    The first line of the input gives the number of test cases T.Then T test cases follow.

    The first line of each test case contains an integer N,indicating the number of players.

    Then follows N lines,i-th line contains an integer x and a string S,indicating the i-th players tell you,"Player x is a S."

    limits:

    1T10

    1N100,000

    1xN

    S {"villager"."werewolf"}
     
    Output
    For each test case,print the number of type-1 players and the number of type-2 players in one line, separated by white space.
     
    Sample Input
    1 2 2 werewolf 1 werewolf
     
    Sample Output
    0 0
     

    Source

    思路:把狼边和人边分别记录,人边用并查集维护,最后判断狼边两端点是否在同一联通块,是则统计指向狼的人边的端点个数。
    PS :统计过程是由dfs和建反向边实现的。
    代码:
     1 #include"bits/stdc++.h"
     2 
     3 #define db double
     4 #define ll long long
     5 #define vl vector<ll>
     6 #define ci(x) scanf("%d",&x)
     7 #define cd(x) scanf("%lf",&x)
     8 #define cl(x) scanf("%lld",&x)
     9 #define pi(x) printf("%d
    ",x)
    10 #define pd(x) printf("%f
    ",x)
    11 #define pl(x) printf("%lld
    ",x)
    12 #define rep(i, n) for(int i=0;i<n;i++)
    13 using namespace std;
    14 const int N = 1e6 + 5;
    15 const int mod = 1e9 + 7;
    16 const int MOD = 998244353;
    17 const db  PI = acos(-1.0);
    18 const db  eps = 1e-10;
    19 const ll  INF = 0x3fffffffffffffff;
    20 int n,t;
    21 int cnt,id;
    22 struct P{int nx,to;}e[N];
    23 struct PP{int fm,to;}g[N];
    24 int head[N];
    25 bool vis[N];
    26 int fa[N];
    27 char s[20];
    28 int sum[N];
    29 int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
    30 void add(int u,int v)
    31 {
    32     e[id].to = u;
    33     e[id].nx = head[v];
    34     head[v] = id++;
    35 }
    36 void unio(int x,int y){
    37     int xx,yy;
    38     xx=find(x),yy=find(y);
    39     if(xx!=yy) fa[xx]=yy;
    40 }
    41 
    42 int dfs(int u)
    43 {
    44     vis[u]=1;
    45     for(int i=head[u];~i;i=e[i].nx){
    46         int v=e[i].to;
    47         if(vis[v]) sum[u]+=sum[v];
    48         else sum[u]+=dfs(v);
    49     }
    50     return sum[u];
    51 }
    52 int main(){
    53     ci(t);
    54     while(t--)
    55     {
    56         ci(n);
    57         cnt=id=0;
    58         memset(head,-1, sizeof(head));
    59         memset(vis,0, sizeof(vis));
    60         for(int i=0;i<n;i++) fa[i]=i,sum[i]=1;//初始化
    61         for(int i=0;i<n;i++){
    62             int u;
    63             scanf("%d %s",&u,s);
    64             u--;
    65             if(s[0]=='w') g[cnt++]={i,u};
    66             else{
    67                 unio(i,u);
    68                 add(i,u);//反向边
    69             }
    70         }
    71         int ans=0;
    72         for(int i=0;i<n;i++) if(!vis[i]) dfs(i);//统计每个点的子节点数
    73         for(int i=0;i<cnt;i++){
    74             if(find(g[i].fm)==find(g[i].to)) ans+=sum[g[i].to];
    75         }
    76         printf("0 %d
    ",ans);
    77     }
    78     return 0;
    79 }
     
  • 相关阅读:
    数据结构Java版之基数排序(四)
    数据结构Java版之递归与迭代算法(五)
    SpringBoot项目在新电脑上的配置运行,包括JDK+MAVEN+Git+SpringBoot配置等
    .NET 控制Windows文件和目录访问权限研究(FileSystemAccessRule)
    Navicat Premium 12.0.18安装与激活
    Spring Data JPA方法定义规范
    在c/c++中调用Java方法
    在Java中调用C/C++本地库
    XP环境下C# 调用Pocess.start()时提示文件找不到的错误解决办法
    C++ 获取当前正在执行的函数的相关信息
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/9543039.html
Copyright © 2011-2022 走看看