提出 #62358247


ソースコード 拡げる

#include<bits/stdc++.h>
using namespace std;

#define UFLIMIT (2<<17)
int unicnt[UFLIMIT];
vector<vector<array<int,3>>>amari; // add
void ufinit(int n){
	for(int i=0;i<n;i++)unicnt[i]=1;
	amari.resize(n); // add
}
int ufroot(int x){return unicnt[x]<=0?-(unicnt[x]=-ufroot(-unicnt[x])):x;}
int ufsame(int x,int y){return ufroot(x)==ufroot(y);}
void uni(int x,int y){
	if((x=ufroot(x))==(y=ufroot(y)))return;
	if(unicnt[x]<unicnt[y])swap(x,y);
	unicnt[x]+=unicnt[y];
	unicnt[y]=-x;
	amari[x].insert(amari[x].end(),amari[y].begin(),amari[y].end()); // add
	amari[y].clear(); // add
}
#undef UFLIMIT


int main(){
	int n,m;
	cin >> n >> m;

	ufinit(n);
	for(int i=0;i<m;i++){
		int x,y;
		cin >> x >> y;
		x--,y--;
		if(ufsame(x,y)){
			amari[ufroot(x)].push_back({x,y,i});
		}else{
			uni(x,y);
		}
	}

	vector<pair<int,int>>cc;
	for(int i=0;i<n;i++)if(ufroot(i)==i)cc.push_back({amari[i].size(),i});
	sort(cc.rbegin(),cc.rend());
	
	cout << cc.size()-1 << endl;

	int pos=1;
	for(auto[_, root]:cc){
		for(auto[x,y,i]:amari[root]){
			if(pos<cc.size()){
				cout << i+1 << ' ' << x+1 << ' ' << cc[pos++].second+1 << endl;
			}
		}
	}
}

提出情報

提出日時
問題 E - Cables and Servers
ユーザ kyopro_friends
言語 C++ 20 (gcc 12.2)
得点 450
コード長 1187 Byte
結果 AC
実行時間 289 ms
メモリ 15888 KiB

コンパイルエラー

Main.cpp: In function ‘int main()’:
Main.cpp:49:31: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   49 |                         if(pos<cc.size()){
      |                            ~~~^~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 450 / 450
結果
AC × 3
AC × 22
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All 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, sample_01.txt, sample_02.txt, sample_03.txt
ケース名 結果 実行時間 メモリ
random_01.txt AC 47 ms 6076 KiB
random_02.txt AC 36 ms 5940 KiB
random_03.txt AC 6 ms 3608 KiB
random_04.txt AC 1 ms 3696 KiB
random_05.txt AC 60 ms 7188 KiB
random_06.txt AC 37 ms 6236 KiB
random_07.txt AC 10 ms 3772 KiB
random_08.txt AC 1 ms 3680 KiB
random_09.txt AC 61 ms 6908 KiB
random_10.txt AC 24 ms 4928 KiB
random_11.txt AC 98 ms 8164 KiB
random_12.txt AC 1 ms 3468 KiB
random_13.txt AC 90 ms 8724 KiB
random_14.txt AC 93 ms 8648 KiB
random_15.txt AC 88 ms 8752 KiB
random_16.txt AC 96 ms 8688 KiB
random_17.txt AC 215 ms 12812 KiB
random_18.txt AC 289 ms 15888 KiB
random_19.txt AC 2 ms 3528 KiB
sample_01.txt AC 1 ms 3488 KiB
sample_02.txt AC 1 ms 3536 KiB
sample_03.txt AC 1 ms 3540 KiB