QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#203501 | #2475. Bank Robbery | PhantomThreshold# | TL | 1ms | 3880kb | C++20 | 3.0kb | 2023-10-06 17:53:18 | 2023-10-06 17:53:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
int n,k;
cin>>n>>k;
vector<vector<int>> G(n+5);
vector<int> deg(n+5);
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
deg[u]++;deg[v]++;
}
int need=0;
for(int i=0;i<n;i++)
{
if(deg[i]!=1)need++;
}
vector<int> good(n+5);
if(k<need+1)
{
cout<<"ATTACK"<<endl;
//interact
for(int i=1;i<=k;i++)
{
int x;
cin>>x;
good[x]=1;
}
while(1)
{
for(int u=0;u<n;u++)
{
int allbad=1;
allbad &= !good[u];
for(auto v:G[u])
{
allbad &= !good[v];
}
if(allbad)
{
cout<<u<<endl;
return 0;
}
}
vector<int> vis(n+5),siz(n+5),gsiz(n+5),pa(n+5,-1),emp(n+5,0);
function<void(int)> dfs=[&](int u)
{
siz[u]=(deg[u]!=1);
gsiz[u]=good[u];
vis[u]=1;
emp[u]=(deg[u]!=1 and not good[u]);
for(auto v:G[u])
{
if(not vis[v])
{
pa[v]=u;
dfs(v);
siz[u]+=siz[v];
gsiz[u]+=gsiz[v];
emp[u]|=emp[v];
}
}
};
int found=0;
for(int x=0;x<n;x++)
{
if(deg[x]!=1 and good[x]==0)
{
fill(pa.begin(),pa.end(),-1);
fill(vis.begin(),vis.end(),0);
fill(siz.begin(),siz.end(),0);
fill(gsiz.begin(),gsiz.end(),0);
dfs(x);
for(int i=0;i<n;i++)
{
if(deg[i]==1)
{
int tt=i;
while(pa[tt]!=x)
tt=pa[tt];
if(not emp[tt] and gsiz[tt]<=siz[tt])
{
cout<<i<<endl;
found=1;
break;
}
}
}
if(found)break;
}
}
if(not found)
{
for(int i=0;i<n;i++)
{
if(deg[i]==1)
{
cout<<i<<endl;
break;
}
}
}
int chg;
cin>>chg;
vector<int> ngood(n+5);
for(int i=0;i<chg;i++)
{
int u,v;
cin>>u>>v;
ngood[v]=1;
good[u]=0;
}
for(int i=0;i<n;i++)
good[i]|=ngood[i];
}
}
else
{
cout<<"DEFEND"<<endl;
int now=0;
for(int i=0;i<n;i++)
{
if(deg[i]!=1)
{
cout<<i<<' ';
good[i]=1;
now++;
}
}
for(int i=0;i<n;i++)
{
if(now==k)break;
if(deg[i]==1)
{
cout<<i<<' ';
good[i]=1;
now++;
}
}
cout<<endl;
//interact
vector<int> pa(n+5,-1);
function<void(int)> dfs=[&](int u)
{
for(auto v:G[u])
{
if(pa[u]==v)continue;
pa[v]=u;
dfs(v);
}
};
int x;
while(cin>>x and x!=-1)
{
if(good[x]){cout<<0<<endl;}
else
{
fill(pa.begin(),pa.end(),-1);
dfs(x);
vector<pair<int,int>> mv;
for(int i=0;i<n;i++)
{
if(deg[i]==1 and good[i]==1)
{
int u=i;
while(pa[u]!=-1)
{
mv.emplace_back(u,pa[u]);
u=pa[u];
}
good[i]=0;
good[x]=1;
break;
}
}
reverse(mv.begin(),mv.end());
cout<<mv.size()<<endl;
for(auto [u,v]:mv)
cout<<u<<' '<<v<<endl;
}
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3880kb
input:
6 3 0 1 0 2 0 3 3 4 3 5 4 2 5 1 2 5 1 5 1 2 1 2 1 5 4 2 5 1 2 5 4 5 2 1 4 5 2 5 1 2 4 2 4 5 1 2 5 4 5 4 1 5 1 2 5 2 5 1 2 4 5 4 2 1 5 1 2 1 4 5 4 2 5 4 1 2 4 2 4 1 2 5 1 2 4 2 1 5 4 5 2 1 2 4 1 4 5 4 5 2 4 5 4 2 5 2 1 5 4 5 2 4 5 1 5 2 5 2 5 1 4 2 1 4 1 5 4 5 2 4 5 1 5 2 1 2 1 4 1 4 2 1 5 1 4 1 2 5 ...
output:
DEFEND 0 3 1 3 3 4 0 3 1 0 3 0 2 3 0 4 3 3 3 5 0 3 2 0 3 0 1 3 0 5 3 2 0 2 1 0 3 3 5 0 3 2 0 3 0 1 3 0 5 3 3 3 5 0 3 1 0 3 0 1 3 0 5 3 2 0 2 1 0 2 0 1 2 0 2 0 2 1 0 2 0 1 2 0 3 3 5 0 3 1 0 2 3 4 5 3 3 0 2 3 0 4 3 3 3 5 0 3 2 0 3 0 1 3 0 5 3 2 0 2 1 0 3 3 5 0 3 2 0 2 3 4 5 3 2 3 5 4 3 3 0 2 3 0 5 3 ...
result:
ok
Test #2:
score: 0
Accepted
time: 1ms
memory: 3592kb
input:
6 2 0 1 0 2 0 3 3 4 3 5 0 3 1 0 1
output:
ATTACK 1 2
result:
ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
100 50 0 1 0 2 0 3 1 4 1 5 2 6 2 7 3 8 3 9 4 10 4 11 5 12 5 13 6 14 6 15 7 16 7 17 8 18 8 19 9 20 9 21 10 22 10 23 11 24 11 25 12 26 12 27 13 28 13 29 14 30 14 31 15 32 15 33 16 34 16 35 17 36 17 37 18 38 18 39 19 40 19 41 20 42 20 43 21 44 21 45 22 46 22 47 23 48 23 49 24 50 24 51 25 52 25 53 26 54...
output:
DEFEND 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 10 34 71 16 34 7 16 2 7 0 2 1 0 4 1 10 4 23 10 49 23 6 36 74 17 36 7 17 16 7 34 16 71 34 2 36 75 74 36 11 46 94 22 46 10 22 4 10 1 4 0 1 2 0 7 2 17 7 36...
result:
ok
Test #4:
score: -100
Time Limit Exceeded
input:
100 49 0 1 0 2 0 3 1 4 1 5 2 6 2 7 3 8 3 9 4 10 4 11 5 12 5 13 6 14 6 15 7 16 7 17 8 18 8 19 9 20 9 21 10 22 10 23 11 24 11 25 12 26 12 27 13 28 13 29 14 30 14 31 15 32 15 33 16 34 16 35 17 36 17 37 18 38 18 39 19 40 19 41 20 42 20 43 21 44 21 45 22 46 22 47 23 48 23 49 24 50 24 51 25 52 25 53 26 54...
output:
ATTACK 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62 49 62...