Submission #54179100
Source Code Expand
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <bits/stdc++.h>
using namespace std;
using ui = unsigned;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,l,r) for(int i=(l);i>=(r);--i)
#define repn(i,n) for(int i=0;i<(n);++i)
#define sizc(x) ((int)x.size())
#define allc(x) x.begin(),x.end()
#define fir first
#define sec second
namespace KnownError_{
constexpr int N = 2e5+5;
int n;
set<pair<int,int>> G[N];
int del[N];
int dfn[N],tick;
int dep[N];
int st[20][N];
int dmin(int x,int y){return dfn[x]<dfn[y]?x:y;}
void dfs(int u,int fa){
st[0][dfn[u]=++tick]=fa;
for(auto p:G[u]){
int v=p.fir,w=p.sec;
if(v==fa)continue;
dep[v]=dep[u]+w;
dfs(v,u);
}
}
int lca(int u,int v){
if(u==v)return u;
if(dfn[u]>dfn[v])swap(u,v);
int k=__lg(dfn[v]-dfn[u]);
return dmin(st[k][dfn[u]+1],st[k][dfn[v]-(1<<k)+1]);
}
int dist(int u,int v){return dep[u]+dep[v]-2*dep[lca(u,v)];}
void main(){
cin>>n;
rep(i,2,n){
int u,v;
cin>>u>>v;
G[u].emplace(v,1);
G[v].emplace(u,1);
}
vector<int> vt;
rep(i,1,n)if(G[i].size()==2)vt.push_back(i);
if(vt.size()==n-2){
cout<<n-1<<'\n';
return;
}
for(auto i:vt){
del[i]=1;
auto p1=*G[i].begin(),p2=*G[i].rbegin();
set<pair<int,int>>().swap(G[i]);
G[p1.fir].erase({i,p1.sec});
G[p2.fir].erase({i,p2.sec});
G[p1.fir].insert({p2.fir,p1.sec+p2.sec});
G[p2.fir].insert({p1.fir,p1.sec+p2.sec});
}
rep(i,1,n)if(!del[i]){dfs(i,0);break;}
for(int j=1;(1<<j)<=tick;++j)
rep(i,1,tick-(1<<j)+1)st[j][i]=dmin(st[j-1][i],st[j-1][i+(1<<j-1)]);
vector<int>().swap(vt);
int sum=0;
rep(i,1,n)if(!del[i]&&G[i].size()==1){
int p=G[i].begin()->fir;
sum+=G[i].begin()->sec;
vt.push_back(p);
}
sort(allc(vt)),vt.erase(unique(allc(vt)),vt.end());
int p=vt[0];
for(auto i:vt)if(dist(vt[0],i)>dist(vt[0],p))p=i;
int q=vt[0];
for(auto i:vt)if(dist(p,i)>dist(p,q))q=i;
int ans=1e9;
rep(i,1,n)if(!del[i]){
int now=2*(n-1)-sum;
if(G[i].size()==1)now+=G[i].begin()->sec;
now-=max(dist(i,p),dist(i,q));
ans=min(ans,now);
}
cout<<ans<<'\n';
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
KnownError_::main();
}
Submission Info
Submission Time |
|
Task |
D - Portable Gate |
User |
KnownError_ |
Language |
C++ 20 (gcc 12.2) |
Score |
0 |
Code Size |
2841 Byte |
Status |
WA |
Exec Time |
183 ms |
Memory |
49836 KB |
Compile Error
Main.cpp: In function ‘void KnownError_::main()’:
Main.cpp:49:21: warning: comparison of integer expressions of different signedness: ‘std::vector<int>::size_type’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
49 | if(vt.size()==n-2){
| ~~~~~~~~~^~~~~
Main.cpp:64:75: warning: suggest parentheses around ‘-’ inside ‘<<’ [-Wparentheses]
64 | rep(i,1,tick-(1<<j)+1)st[j][i]=dmin(st[j-1][i],st[j-1][i+(1<<j-1)]);
| ~^~
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 700 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00-sample-001.txt, 00-sample-002.txt |
All |
00-sample-001.txt, 00-sample-002.txt, 01-handmade-001.txt, 01-handmade-002.txt, 01-handmade-003.txt, 01-handmade-004.txt, 01-handmade-005.txt, 01-handmade-006.txt, 01-handmade-007.txt, 01-handmade-008.txt, 01-handmade-009.txt, 01-handmade-010.txt, 01-handmade-011.txt, 01-handmade-012.txt, 01-handmade-013.txt, 01-handmade-014.txt, 01-handmade-015.txt, 01-handmade-016.txt, 01-handmade-017.txt, 01-handmade-018.txt, 01-handmade-019.txt, 01-handmade-020.txt, 01-handmade-021.txt, 01-handmade-022.txt, 01-handmade-023.txt, 01-handmade-024.txt, 01-handmade-025.txt, 01-handmade-026.txt, 01-handmade-027.txt, 01-handmade-028.txt, 01-handmade-029.txt, 01-handmade-030.txt, 01-handmade-031.txt, 01-handmade-032.txt, 01-handmade-033.txt, 01-handmade-034.txt, 01-handmade-035.txt, 02-random-001.txt, 02-random-002.txt, 02-random-003.txt, 02-random-004.txt, 02-random-005.txt, 02-random-006.txt, 02-random-007.txt, 02-random-008.txt, 02-random-009.txt, 02-random-010.txt, 02-random-011.txt, 02-random-012.txt, 02-random-013.txt, 02-random-014.txt, 02-random-015.txt, 02-random-016.txt, 02-random-017.txt, 02-random-018.txt, 02-random-019.txt, 02-random-020.txt, 02-random-021.txt, 02-random-022.txt, 02-random-023.txt, 02-random-024.txt, 02-random-025.txt, 02-random-026.txt, 02-random-027.txt, 02-random-028.txt, 02-random-029.txt, 02-random-030.txt |
Case Name |
Status |
Exec Time |
Memory |
00-sample-001.txt |
AC |
5 ms |
12824 KB |
00-sample-002.txt |
AC |
5 ms |
12880 KB |
01-handmade-001.txt |
AC |
4 ms |
12860 KB |
01-handmade-002.txt |
AC |
4 ms |
12808 KB |
01-handmade-003.txt |
AC |
67 ms |
32364 KB |
01-handmade-004.txt |
AC |
109 ms |
46536 KB |
01-handmade-005.txt |
AC |
122 ms |
47156 KB |
01-handmade-006.txt |
AC |
120 ms |
46984 KB |
01-handmade-007.txt |
AC |
142 ms |
47080 KB |
01-handmade-008.txt |
AC |
156 ms |
46820 KB |
01-handmade-009.txt |
AC |
145 ms |
46824 KB |
01-handmade-010.txt |
WA |
123 ms |
42344 KB |
01-handmade-011.txt |
AC |
120 ms |
49780 KB |
01-handmade-012.txt |
AC |
130 ms |
46628 KB |
01-handmade-013.txt |
AC |
119 ms |
45808 KB |
01-handmade-014.txt |
AC |
141 ms |
46148 KB |
01-handmade-015.txt |
AC |
151 ms |
45736 KB |
01-handmade-016.txt |
AC |
122 ms |
46424 KB |
01-handmade-017.txt |
WA |
127 ms |
44920 KB |
01-handmade-018.txt |
WA |
119 ms |
44136 KB |
01-handmade-019.txt |
WA |
183 ms |
42880 KB |
01-handmade-020.txt |
AC |
134 ms |
45572 KB |
01-handmade-021.txt |
AC |
121 ms |
45888 KB |
01-handmade-022.txt |
AC |
156 ms |
45268 KB |
01-handmade-023.txt |
WA |
123 ms |
46444 KB |
01-handmade-024.txt |
AC |
114 ms |
42680 KB |
01-handmade-025.txt |
AC |
104 ms |
46472 KB |
01-handmade-026.txt |
AC |
108 ms |
46548 KB |
01-handmade-027.txt |
AC |
133 ms |
40520 KB |
01-handmade-028.txt |
AC |
130 ms |
47020 KB |
01-handmade-029.txt |
WA |
149 ms |
38080 KB |
01-handmade-030.txt |
WA |
116 ms |
44908 KB |
01-handmade-031.txt |
WA |
136 ms |
45096 KB |
01-handmade-032.txt |
AC |
131 ms |
42728 KB |
01-handmade-033.txt |
AC |
109 ms |
48096 KB |
01-handmade-034.txt |
AC |
135 ms |
49836 KB |
01-handmade-035.txt |
WA |
118 ms |
47956 KB |
02-random-001.txt |
AC |
5 ms |
12900 KB |
02-random-002.txt |
AC |
4 ms |
12896 KB |
02-random-003.txt |
AC |
5 ms |
12824 KB |
02-random-004.txt |
AC |
5 ms |
12800 KB |
02-random-005.txt |
AC |
5 ms |
12956 KB |
02-random-006.txt |
AC |
5 ms |
12968 KB |
02-random-007.txt |
WA |
4 ms |
12828 KB |
02-random-008.txt |
AC |
4 ms |
12752 KB |
02-random-009.txt |
AC |
5 ms |
12908 KB |
02-random-010.txt |
WA |
4 ms |
12764 KB |
02-random-011.txt |
WA |
7 ms |
13904 KB |
02-random-012.txt |
WA |
6 ms |
13400 KB |
02-random-013.txt |
WA |
7 ms |
13664 KB |
02-random-014.txt |
WA |
6 ms |
13180 KB |
02-random-015.txt |
WA |
5 ms |
13032 KB |
02-random-016.txt |
WA |
75 ms |
31680 KB |
02-random-017.txt |
WA |
20 ms |
17708 KB |
02-random-018.txt |
WA |
44 ms |
24184 KB |
02-random-019.txt |
WA |
68 ms |
30380 KB |
02-random-020.txt |
WA |
106 ms |
38332 KB |
02-random-021.txt |
WA |
22 ms |
18432 KB |
02-random-022.txt |
WA |
80 ms |
33544 KB |
02-random-023.txt |
WA |
58 ms |
28240 KB |
02-random-024.txt |
WA |
71 ms |
31028 KB |
02-random-025.txt |
WA |
111 ms |
39672 KB |
02-random-026.txt |
WA |
21 ms |
18040 KB |
02-random-027.txt |
WA |
113 ms |
37352 KB |
02-random-028.txt |
WA |
14 ms |
15936 KB |
02-random-029.txt |
WA |
54 ms |
26600 KB |
02-random-030.txt |
WA |
27 ms |
19424 KB |