Description
神校XJ之学霸兮,Dzy皇考曰JC。
摄提贞于孟陬兮,惟庚寅Dzy以降。
纷Dzy既有此内美兮,又重之以修能。
遂降临于OI界,欲以神力而凌♂辱众生。
今Dzy有一魞歄图,其上有N座祭坛,又有M条膴蠁边。
时而Dzy狂WA而怒发冲冠,神力外溢,遂有K条膴蠁边灰飞烟灭。
而后俟其日A50题则又令其复原。(可视为立即复原)
然若有祭坛无法相互到达,Dzy之神力便会大减,于是欲知其是否连通。
Input
第一行N,M
接下来M行x,y:表示M条膴蠁边,依次编号
接下来一行Q
接下来Q行:
每行第一个数K而后K个编号c1~cK:表示K条边,编号为c1~cK
为了体现在线,K以及c1~cK均需异或之前回答为连通的个数
Output
对于每个询问输出:连通则为‘Connected’,不连通则为‘Disconnected’
(不加引号)
Sample Input
5 10
2 1
3 2
4 2
5 1
5 3
4 1
4 3
5 2
3 1
5 4
5
1 1
2 7 0 3
6 0 7 4 6
1 2 7
0 5 0 2 13
2 1
3 2
4 2
5 1
5 3
4 1
4 3
5 2
3 1
5 4
5
1 1
2 7 0 3
6 0 7 4 6
1 2 7
0 5 0 2 13
Sample Output
Connected
Connected
Connected
Connected
Disconnected
Connected
Connected
Connected
Disconnected
HINT
HINT
N≤100000 M≤500000 Q≤50000 1≤K≤15
数据保证没有重边与自环
Tip:请学会使用搜索引擎
这是一道放AK的好题,这题真·强制在线。
-------------------------------------------------------------------------------------------------------------
嗯其实因为k也被异或了。所以倒着做就可以了。
然后最后一个询问用并查集维护下就好
P.S 每行结束不止有'\n',还有'\r'
#include<cstdio>
using namespace std;
int a[500001];
int ans[500001];
struct edge
{
int s,t;
}ed[500001];
int sum[500001];
int fa[500001];
int t[500001];
int find(int x)
{
if(fa[x]!=x)
fa[x]=find(fa[x]);
return fa[x];
}
int main()
{
//freopen("dzy.in","r",stdin);
//freopen("dzy.out","w",stdout);
int n,m;
scanf("%d%d",&n,&m);
int i,j;
for(i=1;i<=m;i++)
scanf("%d%d",&ed[i].s,&ed[i].t);
int q;
scanf("%d",&q);
char x;
int xx;
for(i=1;i<=q;i++)
{
scanf("%d",&xx);
scanf("%c",&x);
scanf("%c",&x);
int s=0;
int p=1;
t[p]=0;
while(x!='\n'&&x!='\r')
{
if(x==' ')
{
s++;
p++;
t[p]=0;
}
else
t[p]=t[p]*10+x-'0';
scanf("%c",&x);
}
s++;
ans[i]=xx^s;
if(i==q)
for(j=1;j<=p;j++)
if(t[j]^ans[i]<=m)
sum[t[j]^ans[i]]=1;
}
int last=0;
for(i=2;i<=q;i++)
{
if(ans[i]>last)
printf("Connected\n");
else
printf("Disconnected\n");
last=ans[i];
}
for(i=1;i<=n;i++)
fa[i]=i;
int fx,fy;
for(i=1;i<=m;i++)
{
if(sum[i]%2==0)
{
if(ed[i].s>n||ed[i].s<=0||ed[i].t>n||ed[i].t<=0)
continue;
fx=find(ed[i].s);
fy=find(ed[i].t);
if(fx!=fy)
fa[fx]=fy;
}
}
bool flag=true;
for(i=1;i<=n-1;i++)
{
fx=find(i);
fy=find(i+1);
if(fx!=fy)
{
flag=false;
printf("Disconnected\n");
break;
}
}
if(flag)
printf("Connected\n");
return 0;
}