Colored Sticks (并查集+Trie + 欧拉路)
2024-08-29 21:14:39
Time Limit: 5000MS | Memory Limit: 128000K | |
Total Submissions: 37340 | Accepted: 9796 |
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
Hint
Huge input,scanf is recommended.
Source
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 500008
#define INF 1000000009
#define eps 0.00000001
/*
欧拉路 一笔画问题!
Trie树就是一种节省空间的hash
利用并查集判断是否联通
*/
char a[], b[];
int cnt = , index = , pre[MAXN], degree[MAXN];
typedef struct node
{
bool flag;
int id;
struct node* next[];
}*Tree;
node memory[MAXN];
Tree Newnode()
{
Tree T = &memory[cnt++];
T->flag = false;
T->id = -;
for (int i = ; i < ; i++)
T->next[i] = NULL;
return T;
}
int Insert(char* s, Tree T)
{
int p = ;
while (s[p] != '\0')
{
int k = s[p] - 'a';
if (!T->next[k])
T->next[k] = Newnode();
T = T->next[k];
p++;
}
if (T->flag)
return T->id;
else
{
T->flag = true;
T->id = index++;
return T->id;
}
}
int find(int x)
{
if (pre[x] == -)
return x;
else
return pre[x] = find(pre[x]);
}
void mix(int a, int b)
{
int fa = find(a), fb = find(b);
if (fa != fb)
{
pre[fa] = fb;
}
}
int main()
{
memset(pre, -, sizeof(pre));
memset(degree, , sizeof(degree));
Tree T = Newnode();
while (scanf("%s %s", a, b) != EOF)
{
//if (a[0] == '#') break;
int ia = Insert(a, T), ib = Insert(b, T);
//cout << ia << ' ' << ib << endl;
degree[ia]++;
degree[ib]++;
mix(ia, ib);
}
int s = find(), tmp = ;
for (int i = ; i < index; i++)
{
if (find(i) != s) //存在多个祖先,图为森林,不连通
{
printf("Impossible\n");
return ;
}
if (degree[i] % )
tmp++;
}
if(tmp== || tmp==)
printf("Possible\n");
else
printf("Impossible\n");
}
最新文章
- Makefile 如何轻松搞定
- OC 实例变量(instance var)与属性(@property)的关系 isa指针
- ruby 字符串学习2
- PHP用Array模拟枚举
- iOS网络传输Delegate不被触发的本质原因
- new Date() iso不支持兼容性问题
- LintCode 38. Search a 2D Matrix II
- win64位 apache2.4 php5.4 mysql5.6
- CodeForces 707A Brain&#39;s Photos
- Mysql主从复制(基于Log)
- lua向文件中写入数据,进行记录
- python构造一个freebuf新闻发送脚本
- 细说Android事件传递
- JS中如何获取JSON有多少个字段,JSON子项的个数或叫length
- 如何打通CMDB,实现就近访问
- Linux配置SSH免登录
- 压缩和解压缩(I)
- VMware里的linux系统里的命令行里会有bee的声音,要如何关掉
- Alpha 冲刺 (1/10)
- 20145332 拓展:注入shellcode实验