提出 #66990892


ソースコード 拡げる

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<bits/stdc++.h>
#define int long long
#define jiaa(a,b) {a+=b;if(a>=MOD) a-=MOD;}
#define jian(a,b) {a-=b;if(a<0) a+=MOD;}
using namespace std;
int ksm(int a,int b,int p){
	if(b==0) return 1;
	if(b==1) return a%p;
	int c=ksm(a,b/2,p);
	c=c*c%p;
	if(b%2==1) c=c*a%p;
	return c%p;	
}
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int n,m,q;
int fa[300005],l[300005],r[300005],x[300005]; 
set<int> tu[300005];
int go(int x){
	if(x==fa[x]) return fa[x];
	return fa[x]=go(fa[x]);
}
signed main()
{
	//freopen("filename.in", "r", stdin);
	//freopen("filename.out", "w", stdout);
	n=read(),m=read();
	for(int i=1;i<=m;i++){
		l[i]=read();
		r[i]=read();
		tu[l[i]].insert(r[i]);
		tu[r[i]].insert(l[i]);
	}
	q=read();
	for(int i=1;i<=q;i++){
		x[i]=read();
	}
	for(int i=1;i<=n;i++) fa[i]=i;
	int ans=m;
	for(int i=1;i<=q;i++){
		int u=go(l[x[i]]);
		int v=go(r[x[i]]);
		if(u==v){
			cout<<ans<<'\n';
			continue;
		}
		if(tu[u].size()>tu[v].size()) swap(u,v);
		fa[u]=v;
		for(auto ed:tu[u]){
			if(tu[ed].count(u)){
				ans--;
				tu[ed].erase(u);
			}
			if(ed!=v&&tu[ed].count(v)==0){
				ans++;
				tu[ed].insert(v);
				tu[v].insert(ed);
			}
		}
		cout<<ans<<'\n';
	}
	return 0;
}

提出情報

提出日時
問題 F - Contraction
ユーザ gbrrain
言語 C++ 20 (gcc 12.2)
得点 525
コード長 1521 Byte
結果 AC
実行時間 588 ms
メモリ 67692 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 525 / 525
結果
AC × 1
AC × 45
セット名 テストケース
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 6 ms 17552 KiB
hand_00.txt AC 162 ms 55096 KiB
hand_01.txt AC 333 ms 57004 KiB
hand_02.txt AC 440 ms 55032 KiB
hand_03.txt AC 174 ms 55168 KiB
hand_04.txt AC 272 ms 46208 KiB
hand_05.txt AC 21 ms 19896 KiB
hand_06.txt AC 252 ms 62196 KiB
hand_07.txt AC 21 ms 22360 KiB
hand_08.txt AC 146 ms 55092 KiB
hand_09.txt AC 260 ms 54976 KiB
hand_10.txt AC 330 ms 55044 KiB
hand_11.txt AC 376 ms 55052 KiB
hand_12.txt AC 336 ms 54008 KiB
hand_13.txt AC 303 ms 52944 KiB
random_00.txt AC 247 ms 46132 KiB
random_01.txt AC 428 ms 62564 KiB
random_02.txt AC 286 ms 54004 KiB
random_03.txt AC 321 ms 54852 KiB
random_04.txt AC 522 ms 67548 KiB
random_05.txt AC 331 ms 57892 KiB
random_06.txt AC 277 ms 42016 KiB
random_07.txt AC 395 ms 60484 KiB
random_08.txt AC 283 ms 53828 KiB
random_09.txt AC 411 ms 54852 KiB
random_10.txt AC 362 ms 55868 KiB
random_11.txt AC 367 ms 56200 KiB
random_12.txt AC 253 ms 45576 KiB
random_13.txt AC 455 ms 61828 KiB
random_14.txt AC 350 ms 53804 KiB
random_15.txt AC 379 ms 54912 KiB
random_16.txt AC 588 ms 67692 KiB
random_17.txt AC 360 ms 56644 KiB
random_18.txt AC 307 ms 45560 KiB
random_19.txt AC 380 ms 60432 KiB
random_20.txt AC 294 ms 53760 KiB
random_21.txt AC 384 ms 54812 KiB
random_22.txt AC 274 ms 56884 KiB
random_23.txt AC 308 ms 55952 KiB
random_24.txt AC 301 ms 42224 KiB
random_25.txt AC 374 ms 61848 KiB
random_26.txt AC 331 ms 53828 KiB
random_27.txt AC 416 ms 55000 KiB
random_28.txt AC 228 ms 55204 KiB
random_29.txt AC 380 ms 56232 KiB