3608: 【USACO】The Great Revegetation(伟大的复仇)jzoj题目传送门
时间限制: 1.000 Sec 内存限制: 256 MB
提交: 29 解决: 6
[命题人:][下载数据: 90]
提交状态报告
题目描述
A lengthy drought has left Farmer John's N pastures devoid of grass. However, with the rainy season arriving soon, the time has come to "revegetate". In Farmer John's shed, he has two buckets, each with a different type of grass seed. He wants to plant grass in each of his N pastures, choosing exactly one type of grass to plant in each.
Being a dairy farmer, Farmer John wants to make sure he manages the somewhat particular dietary needs of his MM cows. Each of his M cows has two favorite pastures. Some of his cows have a dietary restriction that they should only eat one type of grass consistently --- Farmer John therefore wants to make sure the same type of grass is planted in the two favorite fields of any such cow. Other cows have a very different dietary restriction, requiring them to eat different types of grass. For those cows, Farmer John of course wants to make sure their two favorite fields contain different grass types.
Please help Farmer John determine the number of different ways he can plant grass in his N pastures.
一场漫长的干旱使农夫约翰的N块牧场上没有草。然而,随着雨季的到来,是时候“重新种植”了。在农夫约翰的小屋里,他有两个桶,每个桶都有不同类型的草籽。他想在他的N个牧场里种上草,每一个牧场都要选一种。
作为一名奶农,农场主约翰想确保他能满足他那几头奶牛的特殊饮食需求。他家的每头奶牛都有两个最喜欢的牧场。他的一些奶牛有一个饮食限制,那就是它们应该只吃一种类型的草——农夫约翰因此想确保在这类奶牛最喜欢的两个田里种植相同类型的草。其他的奶牛有一个非常不同的饮食限制,要求他们吃不同类型的草。对于那些奶牛,农场主约翰当然想确保他们最喜欢的两块田地里有不同的草。
请帮助农夫约翰确定他在N块牧场上植草的不同方式的数量。
输入
The first line of input contains N (2≤N≤105) and M (1≤M≤105). Each of the next M lines contains a character that is either 'S' or 'D', followed by two integers in the range 1…N, describing the pair of pastures that are the two favorites for one of Farmer John's cows. If the character is 'S', this line represents a cow that needs the same type of grass in its two favorite pastures. If the character is 'D', the line represents a cow that needs different grass types.
输入的第一行包含n(2≤n≤105)和m(1≤m≤105)。下一行M中的每一行都包含一个“s”或“d”字符,后跟两个1…n范围内的整数,描述农夫约翰的一头牛最喜欢的两个牧场。如果字符是“s”,则这一行表示一头母牛在其两个最喜爱的牧场中需要相同类型的草。如果字符为“d”,则该行表示需要不同草类型的奶牛。
输出
Output the number of ways Farmer John can plant grass in his N pastures. Please write your answer in binary.
输出农场主约翰在N个牧场上植草的方式。请用二进制写你的答案。
样例
输入 复制
3 2 S 1 2 D 3 2
输出 复制
10
来源/分类
题解:
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=1000005;
int n,m,ans,fa[3*maxn];
int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
void join(int x,int y){int fx=getfa(x),fy=getfa(y); if(fx!=fy) fa[fy]=fx;}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=3*n;++i) fa[i]=i;
for(int i=1;i<=m;++i)
{
char a[3]; int x,y; scanf("%s%d%d",&a,&x,&y);
join(x,y);
if(a[0]=='S') {
if(fa[x+n]==fa[y+2*n]||fa[x+2*n]==fa[y+n]){puts("0"); return 0;}
join(x+n,y+n);
join(x+2*n,y+2*n);
}
else {
if(fa[x+n]==fa[y+n]){puts("0"); return 0;}
join(x+2*n,y+n);
join(x+n,y+2*n);
}
}
for(int i=1;i<=n;++i) if(fa[i]==i) ++ans;
cout<<'1'; while(ans--) cout<<'0';
return 0;
}