POJ 2513 solution
来源:互联网 发布:日本矮黑人种 知乎 编辑:程序博客网 时间:2024/06/05 19:14
题号:POJ 2513
题意描述:给你N个木棒,木棒两端都有不同的颜色,问是否能将这N个木棒首尾相连连成一条直线
算法:本题算法十分明确:首先,统计所有颜色以颜色为节点(这一部分可以用Trie来维护),木棒为边构建一个无向图,然后判断这个无向图有没有欧拉回路(并查集查出所有连通块个数<应等于1>+奇点个数<=2)。
DA:欧拉回路判断,并查集,Trie
Accepted
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;struct Node{ int w; int next[27];}Tree[550001];int Fa[510001];int G[510001];int nm;int n,m;void insert(char *s){ int l1=strlen(s); int now=0; for (int i=0;i<l1;i++){ int ch=s[i]-'a'+1; if (!Tree[now].next[ch]){ Tree[now].next[ch]=++nm; } now=Tree[now].next[ch]; } Tree[now].w=++n;}int check(char *s){ int l1=strlen(s); int now=0; for (int i=0;i<l1;i++){ int ch=s[i]-'a'+1; if (!Tree[now].next[ch]) return 0; now=Tree[now].next[ch]; } return 1;}int FindFather(int x){ if (Fa[x]<0) return x; else return Fa[x]=FindFather(Fa[x]);}void Union(int x,int y){ int fx=FindFather(x); int fy=FindFather(y); if (fx!=fy){ if (Fa[fx]<Fa[fy]){ Fa[fx]+=Fa[fy]; Fa[fy]=fx; } else{ Fa[fy]+=Fa[fx]; Fa[fx]=fy; } }}int find(char *s){ int l1=strlen(s); int now=0; for (int i=0;i<l1;i++){ int ch=s[i]-'a'+1; if (!Tree[now].next[ch]) return 0; now=Tree[now].next[ch]; } return Tree[now].w;}int work(){ for (int i=0;i<=510001;i++) Fa[i]=-1; nm=0; char s1[1001],s2[1001]; while (scanf("%s%s",&s1,&s2)!=EOF){ if (!check(s1)) insert(s1); if (!check(s2)) insert(s2); int x=find(s1); int y=find(s2); Union(x,y); G[x]++;G[y]++; } int pp=0;int xp=0; for (int i=1;i<=n;i++){ if (G[i]%2) xp++; if (Fa[i]<0 && pp) return 0; if (Fa[i]<0 && !pp) pp=1; } if (xp>2) return 0;else return 1;}int main(){ if (work()) printf("Possible\n");else printf("Impossible\n"); //system("pause");}
- POJ 2513 solution
- POJ 3253 Solution Report
- POJ 1160 Solution Report
- POJ 1149 PIGS Solution
- POJ 2391 Ombrophobic Bovines Solution
- poj 3650 The Seven Percent Solution
- poj 3650 The Seven Percent Solution
- POJ 3650:The Seven Percent Solution
- POJ 3650 The Seven Percent Solution G++
- solution of POJ:3187.Backward Digit Sums
- solution of POJ 2376.Cleaning Shifts
- solution of POJ: 1328.Radar Installation
- solution of POJ:3190.Stall Reservations
- POJ 3650 The Seven Percent Solution
- POJ 3650 The Seven Percent Solution
- poj 3650 The Seven Percent Solution
- Solution:
- solution
- 【精心整理】PHP程序员笔试题+答案
- flex拖拽实例
- REST介绍--来自wiki
- linux kernel 2.4 和 2.6 的区别
- 深刻理解 体会思科UCS刀片
- POJ 2513 solution
- Android 线程详解
- Oracle之自定义函数
- @+id 和 @id的区别
- 万恶的C++ template编译机制
- java自定义栈(链表实现)
- SEO-下拉框排名
- POJ 1236
- 自恢复保险丝的简介和选型(LT电子)