【hihocoder】欧拉路径 并查集判连通

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;
int n,m;
const int maxn=1e4+2;
const int maxm=5e4+2;
int degree[maxn];
int fa[maxn];
struct edge{
    int to;
    int nxt;
}e[2*maxm];
int head[maxn];
int tot;
void init(){
    memset(head,-1,sizeof(head));
    tot=0;
    memset(degree,0,sizeof(degree));
}
void add(int u,int v){
    e[tot].to=v;
    e[tot].nxt=head[u];
    head[u]=tot++;
}
int getfa(int u){
    if(fa[u]==u) return u;
    return fa[u]=getfa(fa[u]);
}
void merge(int u,int v){
    int fu=getfa(u);
    int fv=getfa(v);
    if(fu!=fv){
        fa[u]=fa[v];
    }
}

bool judge(){
    for(int i=1;i<=n;i++){
        fa[i]=i;
    }
    for(int u=1;u<=n;u++){
        for(int i=head[u];i!=-1;i=e[i].nxt){
            int v=e[i].to;
            merge(u,v);
        }
    }
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(fa[i]==i) cnt++;
    }
    if(cnt==1) return true;
    return false;
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        init();
        int u,v;
        for(int i=1;i<=m;i++){
            scanf("%d%d",&u,&v);
            degree[u]++;
            degree[v]++;
            add(u,v);
            add(v,u);
        }
        bool connected=judge();
        if(!connected){
            printf("Part\n");
            continue;
        }
        int cnt=0;
        for(int i=1;i<=n;i++){
            if(degree[i]%2) cnt++;
        }
        if(cnt==0||cnt==2) printf("Full\n");
        else printf("Part\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/itcsl/p/9191522.html

时间: 2024-07-31 02:57:20

【hihocoder】欧拉路径 并查集判连通的相关文章

POJ 2513--Colored Sticks【字典树编号 &amp;&amp; 并查集判连通 &amp;&amp; 欧拉路】

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 32351   Accepted: 8536 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a st

Codeforces 278C Learning Languages(并查集) 求连通块

Codeforces 278C Learning Languages(并查集) 求连通块 为什么最后还要getfather 一遍 比如 x 是 y 的父亲 然后你 Union(x,z) 然后 z 变成了 x 父亲 然后 y 的祖先就是错的了 题解 求一个无向图中有几个连通块 sum 特判 一下 如果 每一个人的语言都为 0 则答案为 sum 其他 答案则为 sum - 1 1 #include <bits/stdc++.h> 2 using namespace std ; 3 4 const

hdu 1116 Play on Words 欧拉路径+并查集

Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7791    Accepted Submission(s): 2676 Problem Description Some of the secret doors contain a very interesting word puzzle. The team

HDU2120【并查集判环】

Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 600    Accepted Submission(s): 344 Problem Description ice_cream's world is a rich country, it has many fertile lands. Today,

HDU - 4514 湫湫系列故事——设计风景线(并查集判环+树形DP)

题目链接:https://vjudge.net/problem/HDU-4514 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好.  现在已经勘探确定了n个位置可以用来建设,在它们之间也勘探确定了m条可以设计的路线以及他们的长度.请问是否能够建成环形的风景线?如果不能,风景线最长能够达到多少?  其中,可以兴建的路线均是双向的,他们之间的长度均大于0. Input 测试数据有

C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree (a connected undirected graph without cycles) of nn vertices. Each of the n−1n−1 edges of the tree is col

POJ2513:Colored Sticks(字典树+欧拉路径+并查集)

http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of t

CSUOJ 1601 War (离线并查集求连通块个数)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 130  Solved: 38 [Submit][Status][Web Board] Description AME decided to destroy CH's country. In CH' country, There are N villages, which are numbered from 1 to N. We say two village A and B ar

HDU - 4514 湫湫系列故事——设计风景线(并查集判环)

题目: 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n个位置可以用来建设,在它们之间也勘探确定了m条可以设计的路线以及他们的长度.请问是否能够建成环形的风景线?如果不能,风景线最长能够达到多少? 其中,可以兴建的路线均是双向的,他们之间的长度均大于0. 思路: 将给出的边的两个端点用并查集放在一起,如果这两个点的祖先相等说明构成了一个环. 在这个用并查