Guilds
Memory limit: 64 MB
King Byteasar faces a serious matter. Two competing trade organisations, The Tailors Guild and The Sewers Guild asked, at the same time, for permissions to open their offices in each town of the kingdom.
There are towns in Byteotia. Some of them are connected with bidirectional roads. Each of the guilds postulate that every town should:
- have an office of the guild, or
- be directly connected to another town that does.
The king, however, suspects foul play. He fears that if there is just a single town holding the offices of both guilds, it could lead to a clothing cartel. For this reason he asks your help.
Input
Two integers, and
(
,
), are given in the first line of the standard input. These denote the number of towns and roads in Byteotia, respectively. The towns are numbered from
to
. Then the roads are given as follows: the input line no.
describes the
-th road; it holds the numbers
and
(
,
), denoting that the
-th road connects the towns
and
. Every pair of towns is (directly) connected by at most one road. The roads do not cross - meeting only in towns - but may lead through tunnels and overpasses.
Output
Your program should print out one word in the first line of the standard output: TAK (yes in Polish) - if the offices can be placed in towns according to these rules, or NIE (no in Polish) - in the opposite case. If the answers is TAK, then the following lines should give an exemplary placement of the offices. Thus the line no.
should hold:
- the letter K if there should be an office of The Tailors Guild in the town
, or
- the letter S if there should be an office of The Sewers Guild in the town
, or
- the letter N if there should be no office in the town
.
Example
For the input data:
7 8 1 2 3 4 5 4 6 4 7 4 5 6 5 7 6 7
the correct result is:
TAK K S K S K K N
The towns in which an office of The Tailors Guild should open are marked with circles, whereas those in which an office of The Sewers Guild should open are marked with rhombi.
Task author: Marcin Pilipczuk.
题目大意
给你一张图,并对图中的一些点进行红黑染色,要求:
1、对于每个红色的点一定有黑色点与其相连,
2、对于每个黑色的点一定有红色点与其相连,
3、对于每个未染色的点一定有红色点和黑色点与其相连。
判断这个图是否有一个染色的可行解,若有,输出一个解
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<vector> #include<queue> using namespace std; vector<int> e[200005]; int n,m,fa[200005],siz[200005],col[200005]; bool vis[200005]; int find(int x) { if(x!=fa[x]) fa[x]=find(fa[x]); return fa[x]; } void dfs(int u,int dep) { vis[u]=1; if(dep%2==1) col[u]=1; else col[u]=2; for(int i=0;i<e[u].size();i++) { int v=e[u][i]; if(vis[v]) continue; dfs(v,dep+1); } } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) fa[i]=i,siz[i]=1; for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); e[x].push_back(y); e[y].push_back(x); int fx,fy; fx=find(x),fy=find(y); if(fx!=fy) { fa[fx]=fy; siz[fy]+=siz[fx]; } } for(int i=1;i<=n;i++) { if(siz[find(i)]==1) { printf("NIE"); return 0; } } for(int i=1;i<=n;i++) { if(fa[i]==i) dfs(i,0); } printf("TAK "); for(int i=1;i<=n;i++) { if(!col[i]) printf("N "); else if(col[i]==1) printf("K "); else printf("S "); } return 0; }