poj2513——判断是否为欧拉图(并查集,trie树)

POJ 2513    Colored Sticks

欧拉回路判定,并查集,trie树

Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 31621   Accepted: 8370

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 the same color?

Input

Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.

Output

If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.

Sample Input

blue red
red violet
cyan blue
blue magenta
magenta cyan

Sample Output

Possible

题意:给若干根两端涂上不同颜色的筷子,相同颜色的筷子端点可接在一起,问是否能将筷子接成一条直线思路:将每种颜色看成结点,筷子看成边,建无向图,判断无向图是否存在欧拉回路。判断欧拉回路:用并查集判断图是否连通,判断是否存在超过两个奇点(入度为奇数的点)难点:题目卡了map和hash。。对字符串的转化只能用trie树了。。。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>

using namespace std;

const int maxn=510010;

int cnt=0;
int indeg[maxn];
int fa[maxn];

struct Node
{
    int id;
    Node *next[26];
};Node root;

int insert(char*s)   //返回结点的id
{
    Node *p=&root;
    for(int i=0;i<strlen(s);i++){
        if(p->next[s[i]-‘a‘]==NULL){
            Node *newnode=(Node*)malloc(sizeof(Node));
            memset(newnode,0,sizeof(Node));
            p->next[s[i]-‘a‘]=newnode;
        }
        p=p->next[s[i]-‘a‘];
    }
    if(p->id) return p->id;  //如果结点已存在,直接返回
    return p->id=++cnt;   //不存在则返回新结点
}

int find(int x)
{
    return fa[x]==x?x:fa[x]=find(fa[x]);
}

int main()
{
    char s1[20],s2[20];
    memset(indeg,0,sizeof(indeg));
    for(int i=0;i<maxn;i++) fa[i]=i;
    char s[40];
    while(gets(s)&&strlen(s)){
        sscanf(s,"%s%s",s1,s2);
        int u=insert(s1),v=insert(s2);
        indeg[u]++;indeg[v]++;    //记录入度
        int x=find(u),y=find(v);
        if(x!=y) fa[x]=y;     //记录合并连通分量
    }
    int n=0;
    bool flag=1;
    for(int i=1;i<=cnt;i++){
        if(indeg[i]&1) n++;
        if(n>2){      //判断奇点数目
            flag=0;break;
        }
        if(find(i)!=find(1)){  //判断连通性
            flag=0;break;
        }
    }
    if(flag) cout<<"Possible"<<endl;
    else cout<<"Impossible"<<endl;
    return 0;
}

poj2513

时间: 2024-08-05 06:51:54

poj2513——判断是否为欧拉图(并查集,trie树)的相关文章

poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断

点击打开链接题目链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 30273   Accepted: 8002 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 sti

POJ - Colored Sticks - 并查集+字典树

这道题主要还是要判断是不是欧拉图 说白了就是能不能这幅图能不能用一笔画下来,那么就可以知道了,如果是一个环状的,说明奇数度就不存在,否则就只能用两个奇数度(起点终点)//我的理解这是 只需要用字典树将单词变为对应的一个数字,然后并查集操作就可以,需要维护一个度变量 #include<stdio.h> #include<string.h> int du[500010],p[500010]; int tot=1; struct tree { tree *next[30]; int id

poj 2513 Colored Sticks(欧拉通路+并查集+字典树)

题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色,现在给定每个木棍两端的颜色,不同木棍之间拼接需要颜色相同的 端才可以,问最后能否将N个木棍拼接在一起. 解题思路:欧拉通路+并查集+字典树.欧拉通路,每个节点的统计度,度为奇数的点不能超过2个.并查集,判断节点 是否完全联通.字典树,映射颜色. #include <cstdio> #include <cstring> #include <string> #includ

测试赛A - Colored Sticks(并查集+字典树+欧拉回路)

A - Colored Sticks Time Limit:5000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64u Submit Status 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 sti

UVA1455 - Kingdom(并查集 + 线段树)

UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum (浮点数小数点一定是0.5) 查询y = fnum这条直线穿过了多少个州和city.州指的是连通的城市. 解题思路:用并查集记录城市之间是否连通,还有每一个州的y的上下界.建立坐标y的线段树,然后每次运行road操作的时候,对范围内的y坐标进行更新:更新须要分三种情况:两个州是相离,还是相交,还是包

hdu 3172 Virtual Friends (并查集 + 字典树)

题目: 链接:点击打开链接 题意: 输入n,给出n行数据,每行有两个字符串,输出关系网络中朋友的个数,n行. 思路: 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int N = 22; const int M = 200020; struct node { int c; node *chil

POJ2513(欧拉回路+并查集+字典树)

Colored Sticks 题目链接:https://vjudge.net/problem/POJ-2513 题目大意: candidate19有好多根棍子,这些棍子的两端分都别涂了一种颜色. candidate19突然有了一个疑问,就是他手里的这些棍子能否互相拼接,从而形成一条直线呢? 两根棍子只有在颜色相同的时候才能拼接.比如有两根棍子,第一根棍子的两端的颜色分别为blue green,第二根两端的颜色为blue red,那么他们就可以拼接成green blue blue red或者red

poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are

[BZOJ2733] [HNOI2012]永无乡(并查集 + 线段树合并)

传送门 一看到第k大就肯定要想到什么权值线段树,主席树,平衡树之类的 然后就简单了 用并查集判断连通,每个节点建立一颗权值线段树,连通的时候直接合并即可 查询时再二分递归地查找 时间复杂度好像不是很稳定...但hzwer都用这种方法水过.. 正解好像是平衡树+启发式合并,以后学TT #include <cstdio> #include <iostream> #define N 100001 int n, m, q, cnt; int a[N], f[N], sum[N * 20],