QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#632107 | #9230. Routing K-Codes | Mu_Silk# | WA | 119ms | 21568kb | C++20 | 2.5kb | 2024-10-12 12:13:29 | 2024-10-12 12:13:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
const ll inf=9e18;
int n,m;
pair<int,int> e[N];
vector<int> son[N];
int deg[N];
ll k[N],b[N],ans;
int u[N],v[N],w[N];
void dfs1(int x,int fa){
k[x]=1;
b[x]=0;
for(auto y:son[x]){
if(y==fa) continue;
dfs1(y,x);
if(v[x]) w[x]=y;
else if(u[x]) v[x]=y;
else u[x]=y;
}
if(k[u[x]]<k[v[x]]) swap(u[x],v[x]);
if(k[u[x]]<k[w[x]]) swap(u[x],w[x]);
int o=u[x],p=v[x],q=w[x];
k[x]+=2ll*(k[o]+k[p]+k[q]);
b[x]+=(b[o]+b[p]+b[q])+(k[p]+k[q]);
// if(!(p+q)) b[x]+=1;
// cout<<"? "<<x<<" "<<k[x]<<" "<<b[x]<<"\n";
}
void dfs2(int x,int fa){
// cout<<"? "<<x<<" "<<k[x]<<" "<<b[x]<<" "<<u[x]<<"\n";
for(auto y:son[x]){
if(y==fa) continue;
ll old_k_x=k[x],old_b_x=b[x];
k[x]-=2ll*k[y],b[x]-=b[y];
if(y==u[x]){
if(w[x])
b[x]-=max(k[v[x]],k[w[x]]);
else b[x]-=k[v[x]];
}else{
b[x]-=k[y];
}
// cout<<"# "<<k[x]<<" "<<k[y]<<"\n";
ll old_k_y=k[y],old_b_y=b[y];
int o=x,p=u[y],q=v[y];
if(k[o]<k[p]) swap(o,p);
if(k[o]<k[q]) swap(o,q);
k[y]=1+2ll*(k[o]+k[p]+k[q]);
b[y]=(b[o]+b[p]+b[q])+(k[p]+k[q]);
// if(!(p+q)) b[y]+=1;
// cout<<"! "<<y<<" "<<k[y]<<" "<<b[y]<<"|"<<o<<" "<<k[o]<<" "<<b[o]<<"|";
// cout<<ans<<"\n";
if(deg[y]==1) ans=min(ans,k[o]+b[o]);
else if(deg[y]==2) ans=min(ans,k[y]+b[y]);
dfs2(y,x);
k[y]=old_k_y,b[y]=old_b_y;
k[x]=old_k_x,b[x]=old_b_x;
}
}
void solve(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
e[i]=make_pair(x,y);
}
if(m>=n){
cout<<"NIE\n";
return;
}
for(int i=1;i<n;i++){
int x=e[i].first;
int y=e[i].second;
son[x].push_back(y);
son[y].push_back(x);
deg[x]++;
deg[y]++;
}
for(int i=1;i<=n;i++){
if(deg[i]>3){
cout<<"NIE\n";
return;
}
}
dfs1(1,0);
ans=inf;
if(deg[1]==1) ans=min(ans,k[u[1]]+b[u[1]]);
else if(deg[1]==2) ans=min(ans,k[1]+b[1]);
dfs2(1,0);
cout<<ans<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n=1;
// cin>>n;
while(n--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9752kb
input:
4 3 1 2 1 3 1 4
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5700kb
input:
4 6 1 2 2 3 3 4 4 1 1 3 2 4
output:
NIE
result:
ok single line: 'NIE'
Test #3:
score: 0
Accepted
time: 1ms
memory: 5704kb
input:
10 10 9 3 5 10 1 4 10 8 8 3 7 3 9 6 8 6 7 2 4 5
output:
NIE
result:
ok single line: 'NIE'
Test #4:
score: -100
Wrong Answer
time: 119ms
memory: 21568kb
input:
200000 199999 172774 188052 195063 155577 38023 148303 30605 155047 170238 19344 46835 58255 154268 109062 197059 116041 136424 12058 42062 149807 109545 115380 132322 51106 16706 162612 62234 31319 195735 80435 173898 84051 19876 102910 186924 9136 123094 117097 145054 173049 137364 152731 82662 18...
output:
-9113696398284717629
result:
wrong answer 1st lines differ - expected: 'NIE', found: '-9113696398284717629'