uva 1160 X-Plosives

简介: 点击打开链接uva 1160 思路: 并查集 分析: 1 看懂题目之和就是切菜了 代码: #include#include#include#includeusing namespace std;const int MAXN...

点击打开链接uva 1160

思路: 并查集
分析:
1 看懂题目之和就是切菜了

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 100010;

int father[MAXN];

int find(int x){
    if(father[x] != x)
        father[x] = find(father[x]);
    return father[x];
}

int main(){
    int x , y , ans;
    while(scanf("%d" , &x) != EOF){ 
        scanf("%d" , &y);
        for(int i = 0 ; i < MAXN ; i++)
            father[i] = i;
        father[x] = y;
        ans = 0;
        while(scanf("%d" , &x) && x != -1){
            scanf("%d" , &y); 
            int fx = find(x); 
            int fy = find(y);
            if(fx == fy)
                ans++;
            else
                father[fx] = fy;
        }
        printf("%d\n" , ans);            
    }
    return 0;
}



目录
相关文章
|
8月前
UVa11776 - Oh Your Royal Greediness!
UVa11776 - Oh Your Royal Greediness!
36 0
|
8月前
uva10112 Myacm Triangles
uva10112 Myacm Triangles
25 0
|
8月前
uva10152 ShellSort
uva10152 ShellSort
40 0
UVa 10082 WERTYU
Problem Description A common typing error is to place the hands on the keyboard one row to the right of the correct position.
861 0
|
人工智能
uva 305 Joseph
点击打开链接uva 305 思路: 数学+打表 分析: 1 传统的约瑟夫问题是给定n个人和m,每次数m次把当前这个人踢出局,问最后留下的一个人的编号 2 这一题是前k个人是好人,后面k个是坏人。
1009 0
uva 1394 - And Then There Was One
点击打开链接uva 1394 思路: 数学递推 分析: 1 题目是一道变形的约瑟夫环变形问题 2 网上看到一篇很好的数学递推法 问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。
951 0
uva 10054 - The Necklace
点击打开链接uva 10054 思路: 欧拉回路 分析: 1 对于一个无向图来说如果这个图是一个欧拉图,那么必须满足该图是连通的并且每个点的度数都是偶数 2 题目给定n条边的无向图问我们是否是一个欧拉图,是的话输出欧拉图的一条路径 3 ...
821 0
uva 10730 - Antiarithmetic?
点击打开链接uva 10730 思路:枚举等差中项 分析: 1 给定一个n个数的序列判断是否有等差子序列 2 很明显我们如果要判断是否有等差子序列的话,只要去判断是否有长度为3的等差子序列 3 对于n
814 0

热门文章

最新文章