# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
436713 | ScarletS | Friend (IOI14_friend) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
using namespace std;
const int N = 1005;
int mt[N];
vector<int> e[N], v[2];
bitset<N> done, cmp;
void dfs(int c, int x)
{
v[x].push_back(c);
mt[c]=-1;
cmp[c]=1;
for (int i : e[c])
if (!cmp[i])
dfs(i,x^1);
}
bool try_kuhn(int c)
{
if (done[c])
return 0;
done[c]=1;
for (int i : e[c])
if (mt[i]==-1||try_kuhn(mt[i]))
{
mt[i]=c;
return 1;
}
return 0;
}
int findSample(int n, int confidence[], int host[], int protocol[])
{
for (int i=1;i<n;++i)
{
if (protocol[i]==0)
{
e[i].push_back(host[i]);
e[host[i]].push_back(i);
}
else
{
for (int j : e[host[i]])
{
e[i].push_back(j);
e[j].push_back(i);
}
}
}
int tot=0, cur;
for (int i=0;i<n;++i)
if (!cmp[i])
{
v[0].clear();
v[1].clear();
dfs(i,0);
for (int j : v[0])
{
done.reset();
try_kuhn(j);
}
for (int j : v[1])
{
done.reset();
try_kuhn(j);
}
cur=0;
for (int j : v[1])
if (mt[j]!=1)
++cur;
tot+=sz(v[0])+sz(v[1])-cur;
}
return tot;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin>>n;
int c[n];
for (int i=0;i<n;++i)
cin>>c[i];
int h[n];
for (int i=1;i<n;++i)
cin>>h[i];
int p[n];
for (int i=1;i<n;++i)
cin>>p[i];
cout<<findSample(n,c,h,p);
return 0;
}