Submission #34897350
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<(int)n;i++)
int read(){int r;scanf("%d",&r);return r;}
int fa[200010];
int d[200010];
int getfa(int v) { return v == fa[v]?v:(fa[v] = getfa(fa[v]));}
vector<int> arr[200010];
int main(){
int n = read();
int m = read();
rep(i,1,n+1) d[i] = read();
iota(fa+1,fa+n+1,1);
rep(i,0,m){
int u = read();
int v = read();
d[u]--;
d[v]--;
int fu = getfa(u);
int fv = getfa(v);
if(fu == fv){
printf("-1\n");
return 0;
}
fa[fu] = fv;
}
rep(i,1,n+1){
if(d[i] < 0){
printf("-1\n");
return 0;
}
int fi = getfa(i);
rep(t,0,d[i]) arr[fi].push_back(i);
}
vector<int> e1; // equal 1
vector<int> g1; // greater than 1
rep(i,1,n+1) if(i == getfa(i)) {
if(!arr[i].size()){
printf("-1\n");
return 0;
}
(arr[i].size() == 1?e1:g1).push_back(i);
}
vector<pair<int,int>>ans;
while(e1.size() && g1.size()){
int pi = e1.back();
int pj = g1.back();
ans.push_back({arr[pi].back(),arr[pj].back()});
e1.pop_back();
arr[pj].pop_back();
if(arr[pj].size() == 1){ // 大于1变成等于1
g1.pop_back();
e1.push_back(pj);
}
}
if(g1.size() || 2 != (int)e1.size()){
printf("-1\n");
return 0;
}
ans.push_back({arr[e1[0]].back(),arr[e1[1]].back()});
for(auto[u,v]:ans) printf("%d %d\n",u,v);
return 0;
}
Submission Info
Submission Time
2022-09-17 03:03:00+0900
Task
F - Construct Highway
User
cromarmot
Language
C++ (GCC 9.2.1)
Score
500
Code Size
1434 Byte
Status
AC
Exec Time
77 ms
Memory
18928 KiB
Compile Error
./Main.cpp: In function ‘int read()’:
./Main.cpp:4:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
4 | int read(){int r;scanf("%d",&r);return r;}
| ~~~~~^~~~~~~~~
Judge Result
Set Name
Sample
All
Score / Max Score
0 / 0
500 / 500
Status
Set Name
Test Cases
Sample
sample_01.txt, sample_02.txt, sample_03.txt
All
hand_01.txt, hand_02.txt, hand_03.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, sample_01.txt, sample_02.txt, sample_03.txt
Case Name
Status
Exec Time
Memory
hand_01.txt
AC
8 ms
8320 KiB
hand_02.txt
AC
9 ms
8376 KiB
hand_03.txt
AC
8 ms
8208 KiB
random_01.txt
AC
77 ms
17844 KiB
random_02.txt
AC
77 ms
14080 KiB
random_03.txt
AC
70 ms
10936 KiB
random_04.txt
AC
69 ms
18928 KiB
random_05.txt
AC
68 ms
15164 KiB
random_06.txt
AC
70 ms
15736 KiB
random_07.txt
AC
75 ms
17700 KiB
random_08.txt
AC
76 ms
14076 KiB
random_09.txt
AC
70 ms
10852 KiB
random_10.txt
AC
77 ms
18068 KiB
random_11.txt
AC
77 ms
14112 KiB
random_12.txt
AC
75 ms
10864 KiB
random_13.txt
AC
72 ms
18692 KiB
random_14.txt
AC
68 ms
9992 KiB
random_15.txt
AC
49 ms
18432 KiB
random_16.txt
AC
49 ms
18448 KiB
random_17.txt
AC
71 ms
18720 KiB
random_18.txt
AC
48 ms
18636 KiB
random_19.txt
AC
50 ms
18644 KiB
random_20.txt
AC
42 ms
16712 KiB
random_21.txt
AC
64 ms
9884 KiB
random_22.txt
AC
34 ms
12528 KiB
random_23.txt
AC
26 ms
11220 KiB
sample_01.txt
AC
9 ms
8432 KiB
sample_02.txt
AC
9 ms
8364 KiB
sample_03.txt
AC
6 ms
8132 KiB