nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230
题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色必须相同,是否能把它们都连起来
思路:很明显的欧拉路径,但题目给的字符串数据很大,得用字典树存取。
代码如下:
#include "stdio.h"
#include "string.h"
#include "stdlib.h" #define N 505000
int set[N],du[N]; int find(int x)
{
if(set[x]==-1) return x;
return set[x]=find(set[x]);
}
void bing(int a,int b)
{
int fa = find(a);
int fb = find(b);
if(fa!=fb) set[fa] = fb;
} struct node
{
struct node *next[26];
int num;
};
int id; void BFS(node *root);
int Trie(node *root,char *s);
void Date_process(int n)
{
int i=0;
int t1,t2;
char s1[15],s2[15];
node *root = (node *)malloc(sizeof(node));
for(i=0; i<26; ++i)
root->next[i] = NULL;
root->num = -1;
id = 0;
memset(du,0,sizeof(du));
memset(set,-1,sizeof(set)); for(i=0; i<n; ++i)
{
scanf("%s %s",s1,s2);
t1 = Trie(root,s1);
t2 = Trie(root,s2);
du[t1]++;
du[t2]++;
bing(t1,t2);
}
BFS(root);
} int main()
{
int T;
int i,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
getchar();
if(n==0)
{
printf("Possible\n");
continue;
}
Date_process(n);
bool flag = true;
int k = find(0);
for(i=0; i<id; ++i)
{
if(find(i)!=k)
flag = false;
}
if(!flag) //若该图不连通,Impossible
{
printf("Impossible\n");
continue;
}
int ans=0;
for(i=0; i<id; ++i)
{
if(du[i]%2==1)
ans++;
}
if(ans==2 || ans==0) //欧拉回路或者欧拉路径 Possible
printf("Possible\n");
else
printf("Impossible\n");
}
return 0;
} int Trie(node *root,char *s) //字典树
{
int i=0;
node *p=root;
while(s[0]!='\0')
{
if(p->next[s[0]-'a']==NULL)
{
node *tt;
tt = (node *)malloc(sizeof(node));
tt->num = -1;
for(i=0; i<26; ++i) tt->next[i] = NULL;
p->next[s[0]-'a'] = tt;
p = tt;
}
else
p = p->next[s[0]-'a'];
s++;
}
if(p->num==-1)
p->num = id++;
return p->num;
} void BFS(node *root) //深搜释放内存
{
int i;
if(root==NULL) return ;
for(i=0; i<26; i++)
{
if(root->next[i]!=NULL)
BFS(root->next[i]);
}
free(root);
}
nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路的更多相关文章
- 优先队列 + 并查集 + 字典树 + 欧拉回路 + 树状数组 + 线段树 + 线段树点更新 + KMP +AC自动机 + 扫描线
这里给出基本思想和实现代码 . 优先队列 : 曾经做过的一道例题 坦克大战 struct node { int x,y,step; friend bool operator <(no ...
- POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)
题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...
- POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)
https://vjudge.net/problem/POJ-2513 题解转载自:優YoU http://user.qzone.qq.com/289065406/blog/1304742541 题 ...
- POJ 2513 无向欧拉通路+字典树+并查集
题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...
- PKU 2513 Colored Sticks(并查集+Trie树+欧拉路径(回路))
题目大意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相连接的一端必须是同颜色的. 解题思路: 可以用图论中欧拉路的知识来解这道题,首先可以把木棒两端看成节点 ...
- POJ-2513 Colored Sticks---欧拉回路+并查集+字典树
题目链接: https://vjudge.net/problem/POJ-2513 题目大意: 给一些木棍,两端都有颜色,只有两根对应的端点颜色相同才能相接,问能不能把它们接成一根木棍 解题思路: 题 ...
- UVA1455 - Kingdom(并查集 + 线段树)
UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum ...
- 【bzoj5133】[CodePlus2017年12月]白金元首与独舞 并查集+矩阵树定理
题目描述 给定一个 $n\times m$ 的方格图,每个格子有 ↑.↓.←.→,表示从该格子能够走到相邻的哪个格子.有一些格子是空着的,需要填上四者之一,需要满足:最终的方格图中,从任意一个位置出发 ...
- 并查集&线段树&树状数组&排序二叉树
超级无敌巨牛逼并查集(带权并查集)https://vjudge.net/problem/UVALive-4487 带删点的加权并查集 https://vjudge.net/problem/UVA-11 ...
随机推荐
- SQL Server case when 日期字符串转换 多表查询 嵌套子查询
select distinct stu.*, dbo.GetClassNameByStudentCode(stu.Code) as ClassName, dbo.GetCourseNameByStud ...
- 并发式IO的解决方案:多路非阻塞式IO、多路复用、异步IO
在Linux应用编程中的并发式IO的三种解决方案是: (1) 多路非阻塞式IO (2) 多路复用 (3) 异步IO 以下代码将以操作鼠标和键盘为实例来演示. 1. 多路非阻塞式IO 多路非阻塞式IO访 ...
- 后缀数组---New Distinct Substrings
Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...
- Laravel 5如何在中间件中获取路由参数?
以官方文档中间件篇的年龄为例子进行了修改 路由部分 Route::get('test/age/{age}',[ 'middleware' => 'old', 'uses'=>'Test@t ...
- ahjesus C# Flags 位域略说
class Program { [Flags] public enum Week { [Description("星期一")] Monday = << , [Descr ...
- PHP异常与错误处理机制
先区别一下php中错误 与 异常的概念吧 PHP错误:是属于php程序自身的问题,一般是由非法的语法,环境问题导致的,使得编译器无法通过检查,甚至无法运行的情况.平时遇到的warming.notice ...
- ASP.NET WebAPI 14 仿写Filter管道
WebAPI中有设计了几种管道(Channel),大概如下:HttpMessageHandler,ActionFilter管道,ExceptionFilter管道.在三种管道中HttpMessageH ...
- linux tcp/ip编程和windows tcp/ip编程差别以及windows socket编程详解
最近要涉及对接现有应用visual c++开发的tcp客户端,花时间了解了下windows下tcp开发和linux的差别,从开发的角度而言,最大的差别是头文件(早期为了推广尽可能兼容,后面越来越扩展, ...
- SAP内存/ABAP内存/共享内存区别
(1).读取和使用方法不同SAP内存使用SET/GET parameters方法:SET PARAMETER ID 'MAT' field p_matnr.GET PARAMETER ID 'MAT' ...
- Creating External Lists From Code
You can create an external list based on an entity (external content type) defined in SharePoint Bus ...