이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
int n, m;
vector<set<pii>> g, todo; // color, to
vector<int> dsu, sz;
vector<int> dsu2;
vector<int> f;
vector<set<int>> key;
void init(){
g.resize(n);
dsu.resize(n);
sz.resize(n, 1);
iota(iter(dsu), 0);
f.resize(n);
iota(iter(f), 0);
key.resize(n);
todo.resize(n);
dsu2.resize(n);
iota(iter(dsu2), 0);
}
int findDSU(int a){
if(dsu[a] != a) dsu[a] = findDSU(dsu[a]);
return dsu[a];
}
void unionDSU(int a, int b){
a = findDSU(a);
b = findDSU(b);
if(dsu[a] == dsu[b]) return;
if(sz[a] < sz[b]) swap(a, b);
//if(key[a].size() < key[b].size()) key[a].swap(key[b]);
for(int i : key[b]){
set<pii>::iterator it;
while(it = g[a].lower_bound(mp(i, -1)),
it != g[a].end() && it->F == i){
todo[a].insert(*it);
g[a].erase(it);
}
}
for(pii i : todo[b]) todo[a].insert(i);
for(pii i : g[b]){
if(key[a].find(i.F) != key[a].end()){
todo[a].insert(i);
}
else{
g[a].insert(i);
}
}
sz[a] += sz[b];
dsu[b] = a;
}
int findDSU2(int a){
if(dsu2[a] != a) dsu2[a] = findDSU2(dsu2[a]);
return dsu2[a];
}
void unionDSU2(int a, int b){
a = findDSU2(a);
b = findDSU2(b);
dsu2[b] = a;
}
vector<int> find_reachable(vector<int> r, vector<int> u, vector<int> v, vector<int> c){
n = r.size();
m = u.size();
init();
for(int i = 0; i < m; i++){
sz[u[i]]++; sz[v[i]]++;
if(c[i] == r[u[i]]) todo[u[i]].insert(mp(c[i], v[i]));
else g[u[i]].insert(mp(c[i], v[i]));
if(c[i] == r[v[i]]) todo[v[i]].insert(mp(c[i], u[i]));
else g[v[i]].insert(mp(c[i], u[i]));
}
for(int i = 0; i < n; i++){
key[i].insert(r[i]);
if(!todo[i].empty()) f[i] = todo[i].begin()->S;
}
//printv(f, cerr);
for(int i = 0; i < n; i++){
unionDSU2(i, f[i]);
}
vector<bool> vst(n);
auto findcycle = [&](int s){
vector<int> path;
while(!vst[s]){
vst[s] = true;
path.eb(s);
s = f[s];
}
bool ok = false;
for(int i : path){
if(i == s) ok = true;
}
if(ok) while(path.back() != s){
unionDSU(path.back(), s);
path.pob;
}
};
for(int i = 0; i < n; i++){
if(!vst[i]) findcycle(i);
}
/*cerr << "cycle\n";
for(int i = 0; i < n; i++) cerr << findDSU(i) << " ";
cerr << "\n";*/
auto solve = [&](int rt){
rt = findDSU(rt);
while(!todo[rt].empty()){
pii i = *todo[rt].begin();
todo[rt].erase(todo[rt].begin());
int to = findDSU(i.S);
if(findDSU2(rt) != findDSU2(to)){
f[rt] = to;
unionDSU2(rt, to);
break;
}
while(to != rt){
unionDSU(to, rt);
to = findDSU(f[to]);
rt = findDSU(rt);
}
}
};
for(int i = 0; i < n; i++){
if(findDSU(i) != i) continue;
if(findDSU(f[i]) == i) solve(i);
}
vector<int> cnt(n);
for(int i = 0; i < n; i++){
cnt[findDSU(i)]++;
}
int mn = MAX;
for(int i = 0; i < n; i++){
if(findDSU(i) == i && findDSU(f[i]) == i) mn = min(mn, cnt[i]);
}
/*cerr << "end\n";
for(int i = 0; i < n; i++) cerr << findDSU(i) << " ";
cerr << "\n";
for(int i = 0; i < n; i++) cerr << findDSU(f[findDSU(i)]) << " ";
cerr << "\n";
printv(cnt, cerr);*/
vector<int> ans(n);
for(int i = 0; i < n; i++){
int p = findDSU(i);
if(findDSU(f[p]) != p) continue;
if(cnt[findDSU(i)] == mn) ans[i] = 1;
}
return ans;
}
# | 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... |