Submission #62358247
Source Code Expand
#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;
}
}
}
}
Submission Info
Submission Time
2025-02-03 16:54:24+0900
Task
E - Cables and Servers
User
kyopro_friends
Language
C++ 20 (gcc 12.2)
Score
450
Code Size
1187 Byte
Status
AC
Exec Time
289 ms
Memory
15888 KiB
Compile Error
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()){
| ~~~^~~~~~~~~~
Judge Result
Set Name
Sample
All
Score / Max Score
0 / 0
450 / 450
Status
Set Name
Test Cases
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
Case Name
Status
Exec Time
Memory
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