QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#51574 | #4539. Independent Feedback Vertex Set | Amalgadon# | AC ✓ | 318ms | 13592kb | C++17 | 1.4kb | 2022-10-02 18:04:39 | 2022-10-02 18:04:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
vector<int> e[N];
int deg[N],a[N],n,col[N],eu[N],ev[N];
bool vis[N];
long long sum[3];
void init()
{
for(int i=1;i<=n;++i) e[i].clear(),deg[i]=col[i]=vis[i]=0;
memset(sum,0,sizeof(sum));
}
int main()
{
int T; scanf("%d",&T);
while(T--)
{
init();
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%d",&a[i]);
for(int i=4;i<=n;++i)
{
scanf("%d%d",&eu[i],&ev[i]);
deg[i]+=(eu[i]>3),deg[i]+=(ev[i]>3);
e[eu[i]].push_back(i),e[ev[i]].push_back(i);
e[i].push_back(eu[i]),e[i].push_back(ev[i]);
}
queue<int> q;
vis[1]=vis[2]=vis[3]=true;
col[2]=1,col[3]=2;
for(int i=4;i<=n;++i) if(!deg[i])
{
q.push(i);
col[i]=3-col[eu[i]]-col[ev[i]];
vis[i]=true;
}
while(!q.empty())
{
int u=q.front(); q.pop();
for(auto v:e[u]) if(!vis[v])
{
if(!(--deg[v]))
{
col[v]=3-col[eu[v]]-col[ev[v]];
vis[v]=true, q.push(v);
}
}
}
for(int i=1;i<=n;++i) sum[col[i]]+=a[i];
printf("%lld\n",max(sum[0],max(sum[1],sum[2])));
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 318ms
memory: 13592kb
input:
1000 1561 14 78 18 74 80 97 70 49 39 81 79 65 55 1 29 32 91 73 94 41 69 41 79 34 62 81 25 33 76 26 57 69 66 48 37 78 67 50 21 8 27 58 58 32 8 42 31 52 35 36 99 56 61 27 4 64 50 59 43 55 73 53 41 80 94 32 56 59 19 57 28 56 35 73 70 13 18 6 96 47 71 29 16 73 65 27 61 68 50 11 21 96 19 18 33 97 6 43 19...
output:
26477 730 133872218725 206 139 26838 225784601561 409 163907525588 65 27568981002 26830437 612 206 647 3808763 392 39765006630 9823066 71309193 126 10667 93591615 12028 113 12356789 2420359 213 418 223 166 40 5324 219 1419 562 663 22941 14 39 102638547 14199134 58 201 12653791 101 10447 16511 171245...
result:
ok 1000 lines