Submission #44929143
Source Code Expand
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
using namespace std;
const int MAXN = 2e5+10;
int T,n,w[MAXN],deg[MAXN],vis[MAXN],fa[MAXN],sz[MAXN],dep[MAXN],top[MAXN];
vector<int>e[MAXN],leaf;
typedef array<int,2> pr;
vector<pr>ret;
void Init(){
rep(i,1,n){
e[i].clear();
deg[i] = vis[i] = fa[i] = 0;
}
ret.clear();
}
void output(){
cout<<ret.size()<<"\n";
for(auto [u,v] : ret)cout<<u<<" "<<v<<"\n";
/*
ll sum = 0;
for(auto [u,v] : ret)sum += w[u] + w[v];
cout<<sum<<endl;
*/
}
void even_build(){
int sz = leaf.size();
rep(i,1,sz/2)ret.push_back({leaf[i-1],leaf[i+sz/2-1]});
}
void dfs(int u){
sz[u] = 1;dep[u] = 1;
for(auto v:e[u])if(v != fa[u] && !vis[v]){
fa[v] = u;top[v] = top[u];
dfs(v);
sz[u] += sz[v];
dep[u] = max(dep[u],dep[v]+1);
}
if(sz[u]==1)leaf.push_back(u);
}
void solve(){
cin>>n;
Init();
rep(i,1,n)cin>>w[i];
rep(i,1,n-1){
int u,v;cin>>u>>v;
e[u].push_back(v);e[v].push_back(u);
deg[u]++;deg[v]++;
}
int rt = 0;rep(i,1,n)if(deg[i]>1)rt = i;
assert(rt);
leaf.clear();
dfs(rt);
if(leaf.size() % 2 == 0){ //偶数个叶子
even_build();
output();
return;
}
int x = 0,y = 0;
w[0] = 2e9;
rep(i,1,n){
if(w[i] < w[x])y=x,x=i;
else if(w[i]<w[y])y=i;
}
assert(x&&y);
leaf.clear();fa[x] = 0;
for(auto u : e[x]){
fa[u] = x;top[u] = u;
dfs(u);
}
for(auto u : leaf)if(sz[top[u]] != dep[top[u]]){ //+min w
ret.push_back({u,x});
int cnt = n;
for(int p=u;deg[p] != 3;p=fa[p]){
vis[p] = 1;
}
leaf.clear();
int rt = 0;
rep(i,1,n)if(!vis[i] && deg[i] > 1)rt = i;
assert(rt);
fa[rt] = 0;
dfs(rt);
even_build();
output();
return;
}
//必定有三条链
assert(leaf.size() == 3);
for(auto u : leaf)for(auto v : leaf)for(auto w : leaf){
if(u == v || u == w || v == w)continue;
if(top[u] != top[y])continue;
ret.push_back({y,v});
ret.push_back({u,w});
output();
return;
}
assert(0);
}
int main(){
ios::sync_with_stdio(false);
cin>>T;
while(T--)solve();
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | E - Make Biconnected |
| User | Crying |
| Language | C++ (GCC 9.2.1) |
| Score | 800 |
| Code Size | 2458 Byte |
| Status | AC |
| Exec Time | 137 ms |
| Memory | 35572 KB |
Compile Error
./Main.cpp: In function ‘void solve()’:
./Main.cpp:96:7: warning: unused variable ‘cnt’ [-Wunused-variable]
96 | int cnt = n;
| ^~~
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 800 / 800 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt |
| All | 00_sample_00.txt, 01_small_1_00.txt, 01_small_1_01.txt, 01_small_1_02.txt, 01_small_1_03.txt, 01_small_1_04.txt, 01_small_1_05.txt, 01_small_1_06.txt, 01_small_1_07.txt, 01_small_1_08.txt, 02_small_2_00.txt, 02_small_2_01.txt, 02_small_2_02.txt, 02_small_2_03.txt, 02_small_2_04.txt, 02_small_2_05.txt, 02_small_2_06.txt, 02_small_2_07.txt, 03_random_1_00.txt, 03_random_1_01.txt, 03_random_1_02.txt, 03_random_1_03.txt, 03_random_1_04.txt, 03_random_1_05.txt, 03_random_1_06.txt, 03_random_1_07.txt, 03_random_1_08.txt, 03_random_1_09.txt, 03_random_1_10.txt, 03_random_1_11.txt, 03_random_1_12.txt, 03_random_1_13.txt, 04_random_2_00.txt, 04_random_2_01.txt, 04_random_2_02.txt, 04_random_2_03.txt, 04_random_2_04.txt, 05_random_3_00.txt, 05_random_3_01.txt, 05_random_3_02.txt, 05_random_3_03.txt, 05_random_3_04.txt, 06_path_00.txt, 06_path_01.txt, 06_path_02.txt, 07_centipede_00.txt, 07_centipede_01.txt, 07_centipede_02.txt, 07_centipede_03.txt, 08_few_leaves_00.txt, 08_few_leaves_01.txt, 08_few_leaves_02.txt, 08_few_leaves_03.txt, 08_few_leaves_04.txt, 08_few_leaves_05.txt, 08_few_leaves_06.txt, 08_few_leaves_07.txt, 08_few_leaves_08.txt, 08_few_leaves_09.txt, 08_few_leaves_10.txt, 08_few_leaves_11.txt, 09_corner_00.txt, 09_corner_01.txt, 09_corner_02.txt, 09_corner_03.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | AC | 9 ms | 8232 KB |
| 01_small_1_00.txt | AC | 92 ms | 8232 KB |
| 01_small_1_01.txt | AC | 88 ms | 8208 KB |
| 01_small_1_02.txt | AC | 88 ms | 8228 KB |
| 01_small_1_03.txt | AC | 87 ms | 8260 KB |
| 01_small_1_04.txt | AC | 89 ms | 8260 KB |
| 01_small_1_05.txt | AC | 89 ms | 8148 KB |
| 01_small_1_06.txt | AC | 88 ms | 8168 KB |
| 01_small_1_07.txt | AC | 88 ms | 8200 KB |
| 01_small_1_08.txt | AC | 57 ms | 8208 KB |
| 02_small_2_00.txt | AC | 83 ms | 8252 KB |
| 02_small_2_01.txt | AC | 81 ms | 8144 KB |
| 02_small_2_02.txt | AC | 74 ms | 8196 KB |
| 02_small_2_03.txt | AC | 79 ms | 8204 KB |
| 02_small_2_04.txt | AC | 70 ms | 8196 KB |
| 02_small_2_05.txt | AC | 70 ms | 8148 KB |
| 02_small_2_06.txt | AC | 67 ms | 8232 KB |
| 02_small_2_07.txt | AC | 69 ms | 8248 KB |
| 03_random_1_00.txt | AC | 114 ms | 8248 KB |
| 03_random_1_01.txt | AC | 93 ms | 8252 KB |
| 03_random_1_02.txt | AC | 79 ms | 8272 KB |
| 03_random_1_03.txt | AC | 70 ms | 8248 KB |
| 03_random_1_04.txt | AC | 65 ms | 8236 KB |
| 03_random_1_05.txt | AC | 63 ms | 8248 KB |
| 03_random_1_06.txt | AC | 62 ms | 8264 KB |
| 03_random_1_07.txt | AC | 66 ms | 8268 KB |
| 03_random_1_08.txt | AC | 64 ms | 8380 KB |
| 03_random_1_09.txt | AC | 67 ms | 8528 KB |
| 03_random_1_10.txt | AC | 69 ms | 8752 KB |
| 03_random_1_11.txt | AC | 74 ms | 9224 KB |
| 03_random_1_12.txt | AC | 76 ms | 10068 KB |
| 03_random_1_13.txt | AC | 89 ms | 11568 KB |
| 04_random_2_00.txt | AC | 135 ms | 20240 KB |
| 04_random_2_01.txt | AC | 133 ms | 20220 KB |
| 04_random_2_02.txt | AC | 127 ms | 20252 KB |
| 04_random_2_03.txt | AC | 131 ms | 20260 KB |
| 04_random_2_04.txt | AC | 101 ms | 20164 KB |
| 05_random_3_00.txt | AC | 96 ms | 20624 KB |
| 05_random_3_01.txt | AC | 124 ms | 20460 KB |
| 05_random_3_02.txt | AC | 96 ms | 20512 KB |
| 05_random_3_03.txt | AC | 96 ms | 20528 KB |
| 05_random_3_04.txt | AC | 117 ms | 20552 KB |
| 06_path_00.txt | AC | 85 ms | 35572 KB |
| 06_path_01.txt | AC | 87 ms | 35520 KB |
| 06_path_02.txt | AC | 108 ms | 31484 KB |
| 07_centipede_00.txt | AC | 136 ms | 26856 KB |
| 07_centipede_01.txt | AC | 108 ms | 24460 KB |
| 07_centipede_02.txt | AC | 115 ms | 24832 KB |
| 07_centipede_03.txt | AC | 134 ms | 28064 KB |
| 08_few_leaves_00.txt | AC | 137 ms | 30088 KB |
| 08_few_leaves_01.txt | AC | 72 ms | 9460 KB |
| 08_few_leaves_02.txt | AC | 122 ms | 26152 KB |
| 08_few_leaves_03.txt | AC | 66 ms | 9344 KB |
| 08_few_leaves_04.txt | AC | 103 ms | 25752 KB |
| 08_few_leaves_05.txt | AC | 59 ms | 9144 KB |
| 08_few_leaves_06.txt | AC | 106 ms | 27628 KB |
| 08_few_leaves_07.txt | AC | 61 ms | 9436 KB |
| 08_few_leaves_08.txt | AC | 134 ms | 23940 KB |
| 08_few_leaves_09.txt | AC | 74 ms | 9280 KB |
| 08_few_leaves_10.txt | AC | 132 ms | 24960 KB |
| 08_few_leaves_11.txt | AC | 73 ms | 9300 KB |
| 09_corner_00.txt | AC | 9 ms | 8196 KB |
| 09_corner_01.txt | AC | 81 ms | 8232 KB |
| 09_corner_02.txt | AC | 80 ms | 8208 KB |
| 09_corner_03.txt | AC | 79 ms | 8208 KB |