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>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
const ll maxn=3e5+10;
const ll mod =998244353 ;
const ll base=3e18;
vector<pll> adj[maxn];
ll a[maxn];
ll cntnw=0;
bool dd[maxn];
ll chk[maxn];
bool h[maxn];
// key that we are having
ll par[maxn];
ll ans[maxn];
ll find_par(ll u)
{
if (par[u]<0)
return u;
return par[u]=find_par(par[u]);
}
void dsu(ll x,ll y)
{
x=find_par(x);
y=find_par(y);
if (dd[y])
{
dd[x]=1;
return ;
}
assert(x!=y);
par[y]+=par[x];
par[x]=y;
}
bool dosth(ll x)
{
cntnw++;
vector<ll> vt;
vt.pb(x);
chk[x]=cntnw;
h[a[x]]=1;
for (int i=0; i<vt.size(); i++)
{
ll u=vt[i];
for (auto p:adj[u])
{
ll to=p.ff;
ll key=p.ss;
if (h[key]&&chk[to]!=cntnw)
{
if (find_par(to)!=x)
{
for (auto to:vt)
h[a[to]]=0;
dsu(x,find_par(to));
return true;
}
chk[to]=cntnw;
vt.pb(to);
h[a[to]]=1;
}
}
}
dd[x]=1;
for (auto to:vt)
h[a[to]]=0,ans[to]=vt.size();
return false;
}
vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c)
{
memset(par,-1,sizeof(par));
memset(ans,0x3f,sizeof(ans));
ll n=r.size();
ll m=u.size();
vector<int> res;
for (int i=1; i<=m; i++)
{
adj[u[i-1]+1].pb(make_pair(v[i-1]+1,c[i-1]));
adj[v[i-1]+1].pb(make_pair(u[i-1]+1,c[i-1]));
}
for (int i=1; i<=n; i++)
a[i]=r[i-1];
while (1)
{
bool chk=false;
for (int i=1; i<=n; i++)
{
if (!dd[i]&&par[i]<0)
{
// cout <<i<<" chk"<<endl;
chk|=dosth(i);
// cout <<i<<endl;
}
}
// cout <<chk<<endl;
if (!chk)
break;
}
ll mn=base;
for (int i=1; i<=n; i++)
mn=min(mn,ans[i]);
for (int i=1; i<=n; i++)
{
if (ans[i]==mn)
res.pb(1);
else
res.pb(0);
}
return res;
}
/*int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("t.inp", "r"))
{
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll n, m;
cin>> n>> m;
vector<int> r;
for (int i=1; i<=n; i++)
{
int x;
cin>> x;
r.pb(x);
}
vector<int> u,v,c;
for (int i=1;i<=m;i++)
{
int x, y, w;
cin>> x>> y>> w;
u.pb(x);
v.pb(y);
c.pb(w);
}
vector<int> ans=find_reachable(r,u,v,c);
for (auto to:ans) cout <<to<<" ";
}*/
/*
4 5
0 1 1 2
0 1 0
0 2 0
1 2 1
1 3 0
3 1 2*/
/*
7 10
0 1 1 2 2 1 2
0 1 0
0 2 0
1 2 1
1 3 0
2 3 0
3 4 1
3 5 2
4 5 0
4 6 2
5 6 1
*/
Compilation message (stderr)
keys.cpp: In function 'bool dosth(long long int)':
keys.cpp:47:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i=0; i<vt.size(); i++)
| ~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |