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

这道题主要还是要判断是不是欧拉图

说白了就是能不能这幅图能不能用一笔画下来,那么就可以知道了,如果是一个环状的,说明奇数度就不存在,否则就只能用两个奇数度(起点终点)//我的理解这是

只需要用字典树将单词变为对应的一个数字,然后并查集操作就可以,需要维护一个度变量

#include<stdio.h>
#include<string.h>
int du[500010],p[500010];
int tot=1;
struct tree
{
	tree *next[30];
	int id;
	tree()
	{
		id=0;
		for(int i=0;i<30;i++)
		{
			next[i]=NULL;
		}
	}
}*root;
int find(int n)
{
	if(p[n]!=n)
	{
		p[n]=find(p[n]);
	}
	return p[n];
}
int set(char *s)
{
	tree *tmp;
	tmp=root;
	for(int i=0;s[i];i++)
	{
		int j=s[i]-'a';
		if(tmp->next[j]==NULL)
		{
			tmp->next[j]=new tree;
		}
		tmp=tmp->next[j];
	}
	if(tmp->id==0)
	{
		return tmp->id=tot++;
	}
	return tmp->id;
}
int main()
{
	root=new tree;
	char color1[50],color2[50];
	int i;
	for(int i=0;i<500010;i++)
	{
		p[i]=i;
		du[i]=0;
	}
	while(scanf("%s%s",color1,color2)!=EOF)
	{
		int c1=set(color1);
		int c2=set(color2);
		du[c1]++;
		du[c2]++;
		int C1=find(c1);
		int C2=find(c2);
		p[C1]=C2;
	}
	int rnum=0,onum=0;
	for(i=1;i<tot;i++)
	{
		if(p[i]==i)
		{
			rnum++;
		}
		if(rnum>1)
		{
			puts("Impossible");
			return 0;
		}
	}
	for(i=1;i<tot;i++)
	{
		if(du[i]&1)
		{
			onum++;
		}
	}
	if(onum==0||onum==2)
	{
		puts("Possible");
	}
	else
	{
		puts("Impossible");
	}
}

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

时间: 2024-07-30 13:49:36

POJ - Colored Sticks - 并查集+字典树的相关文章

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 2513 Colored Sticks(欧拉通路+并查集+字典树)

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

POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)

题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 转:kuangbing 无向图存在欧拉路的充要条件为: ①     图是连通的: ②     所有节点的度为偶数,或者有且只有两个度为奇数的节点. 图的连通可以利用并查集去判断. 度数的统计比较容易. view code//第一次用指针写trie,本来是用二维数组,发现数组开不下,只好删删改改,改成指针 //做这道题,知道了欧拉回路判定,还有用指针写trie #include

测试赛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

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

优先队列 + 并查集 + 字典树 + 树状数组 + 线段树 + 线段树点更新 + KMP +AC自动机 + 扫描线

这里给出基本思想和实现代码 . 优先队列 : 曾经做过的一道例题       坦克大战 1 struct node 2 { 3 int x,y,step; 4 friend bool operator <(node s1,node s2) // 定义结构体 的时候 这个 就是 用于 优先队列的 基准排序 5 { 6 return s1.step>s2.step; // 步数小的 在上 为小顶堆 7 } 8 }; 9 priority_queue<node>Q; // 优先队列的结构

POJ2513 Colored Sticks (并查集+trie)

传送门 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K       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

poj2513Colored Sticks(欧拉通路+字典树+并查集)

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