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 vector<int> vi;
typedef long long ll;
const int mn = 3e5+10;
vector<int> g[mn];
unordered_map<int,vector<int>> uval[mn];
set<int> keys[mn];
void merge(int a, int b) {
for(int k:keys[b])keys[a].insert(k);
g[a].insert(g[a].end(), g[b].begin(), g[b].end());
for(auto& [k,v]: uval[b]) {
if(keys[a].count(k)){
g[a].insert(g[a].end(), v.begin(), v.end());
}
else {
auto &v2 = uval[a][k];
v2.insert(v2.end(), v.begin(), v.end());
}
}
for(int k:keys[b]){
auto &v = uval[a][k];
g[a].insert(g[a].end(), v.begin(), v.end());
uval[a].erase(k);
}
g[b].clear();
uval[b].clear();
keys[b].clear();
}
struct dsu {
vi p, sz;
dsu(int n) : p(n), sz(n, 1) {
for (int i = 0; i < n; i++) p[i] = i;
}
int f(int x) { return p[x]==x ? x : p[x] = f(p[x]); }
void mrg(int a,int b) {
a = f(a), b = f(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
merge(a,b);
p[b] = a;
sz[a] += sz[b];
}
} dsu(mn);
int bans=0x3f3f3f3f;
set<int>ans;
bool vis[mn], ins[mn];
int dfs(int x){
vis[x]=ins[x]=1;
while(g[x].size()){
int y=dsu.f(g[x].back());
g[x].pop_back();
if(x==y)continue;
if(ins[y]){
ins[x]=0;
return y;
}
else if(vis[y]){
ins[x]=0;
return -1;
}
int ret = dfs(y);
if(ret == -1) {
ins[x]=0;
return -1;
}
else{
dsu.mrg(x,y);
if(ret == x){
ins[x]=0;
x = dsu.f(x);
return dfs(x);
}
else {
ins[x]=0;
return ret;
}
}
}
if(dsu.sz[x]<bans)ans.clear();
if(dsu.sz[x]<=bans){
ans.insert(x);
bans=dsu.sz[x];
}
ins[x]=0;
return -1;
}
std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u, std::vector<int> v, std::vector<int> c) {
int n = r.size();
for(int i=0;i<n;i++)keys[i].insert(r[i]);
auto addEdge = [&](int u, int v, int c) {
if(keys[u].count(c))g[u].push_back(v);
else uval[u][c].push_back(v);
};
for(int i=0;i<u.size();i++){
addEdge(u[i],v[i],c[i]);
addEdge(v[i],u[i],c[i]);
}
for(int i=0;i<n;i++)if(!vis[i])dfs(i);
vi ret(n);
for(int i=0;i<n;i++)if(ans.count(dsu.f(i)))ret[i]=1;
return ret;
}
Compilation message (stderr)
keys.cpp: In function 'std::vector<int> find_reachable(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
keys.cpp:103:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for(int i=0;i<u.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... |