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;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
vector<ii>exo[2005];
bool vis[2005];
ll p[2005];
set<ll>ex[2005];
queue<ll>q;
ll cur;
ll find_par(ll a){
if(p[a] == a){
return a;
}
return p[a] = find_par(p[a]);
}
void enose(ll a,ll b){
ll p1 = find_par(a);
ll p2 = find_par(b);
if(p1 != p2){
if(ex[p1].size() > ex[p2].size()){
swap(p1,p2);
}
for(auto x:ex[p1]){
ex[p2].insert(x);
if(p2 == cur){
if(!vis[x]){
vis[x] = 1;
q.push(x);
}
}
}
if(p1 == cur){
for(auto x:ex[p2]){
if(!vis[x]){
vis[x] = 1;
q.push(x);
}
}
}
ex[p1].clear();
}
}
std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u, std::vector<int> v, std::vector<int> c) {
std::vector<int> ans(r.size(), 1);
ll n = r.size();
ll m = u.size();
f(i,0,m){
exo[c[i]].pb(ii(u[i],v[i]));
}
ll timi[n] = {0};
ll mini = n + 100;
f(i,0,n){
f(j,0,n){
vis[j] = 0;
p[j] = j;
ex[j].insert(r[j]);
}
cur = i;
vis[r[i]] = 1;
q.push(r[i]);
while(!q.empty()){
ll f = q.front();
q.pop();
for(auto x:exo[f]){
enose(x.F,x.S);
}
}
timi[i] = ex[find_par(i)].size();
mini = min(mini,timi[i]);
}
f(i,0,n){
if(timi[i] != mini){
ans[i] = 0;
}
}
return ans;
}
/*
int main() {
int n, m;
assert(2 == scanf("%d%d", &n, &m));
std::vector<int> r(n), u(m), v(m), c(m);
for(int i=0; i<n; i++) {
assert(1 == scanf("%d", &r[i]));
}
for(int i=0; i<m; i++) {
assert(3 == scanf("%d %d %d", &u[i], &v[i], &c[i]));
}
fclose(stdin);
std::vector<int> ans = find_reachable(r, u, v, c);
for (int i = 0; i < (int) ans.size(); ++i) {
if(i) putchar(' ');
putchar(ans[i]+'0');
}
printf("\n");
return 0;
}
*/
# | 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... |