Submission #446461

#TimeUsernameProblemLanguageResultExecution timeMemory
446461lohachoKeys (IOI21_keys)C++17
0 / 100
1 ms204 KiB
#include <bits/stdc++.h>

using namespace std;

std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u, std::vector<int> v, std::vector<int> c) {
    int n = (int)r.size(), m = (int)u.size();
    vector<vector<int>> way(n), wayb(n);
    vector<vector<pair<int, int>>> wall(n);
    for(int i = 0; i < m; ++i){
        if(r[u[i]] == c[i]){
            way[u[i]].push_back(v[i]);
            wayb[v[i]].push_back(u[i]);
        }
        wall[u[i]].push_back({v[i], c[i]});
        if(r[v[i]] == c[i]){
            way[v[i]].push_back(u[i]);
            wayb[u[i]].push_back(v[i]);
        }
        wall[v[i]].push_back({u[i], c[i]});
    }
    vector<int> chk(n), chk2(n);
    vector<int> ord;
    auto dfs = [&](auto&&self, int x)->void{
        if(chk[x]) return;
        chk[x] = 1;
        for(auto&nxt:way[x]){
            self(self, nxt);
        }
        ord.push_back(x);
    };
    auto dfs2 = [&](auto&&self, int x)->void{
        if(chk2[x]) return;
        chk[x] = chk2[x] = 1;
        for(auto&nxt:wayb[x]){
            self(self, nxt);
        }
    };
    vector<vector<int>> who(n);
    vector<int> col, same(n, -1), del;
    auto mdfs = [&](auto&&self, int x, int num)->void{
        same[x] = num;
        col.push_back(r[x]);
        for(auto&nxt:wall[x]){
            who[nxt.second].push_back(nxt.first);
            del.push_back(nxt.second);
        }
        for(auto&nxt:way[x]){
            if(same[nxt] == -1){
                self(self, nxt, num);
            }
        }
    };
    for(int i = 0; i < n; ++i){
        if(chk[i]){
            continue;
        }
        dfs(dfs, i);
        int rt;
        while((int)ord.size()){
            if(!chk2[ord.back()]){
                rt = ord.back();
                dfs2(dfs2, ord.back());
            }
            ord.pop_back();
        }
        for(auto&nxt:wall[rt]){
            who[nxt.second].push_back(nxt.first);
            del.push_back(nxt.second);
        }
        col.push_back(r[rt]);
        same[rt] = i;
        while((int)col.size()){
            int now = col.back(); col.pop_back();
            if(!(int)who[now].size()) continue;
            while((int)who[now].size()){
                int nxt = who[now].back(); who[now].pop_back();
                if(same[nxt] != -1) continue;
                mdfs(mdfs, nxt, i);
            }
        }
        while((int)del.size()){
            who[del.back()].clear();
            del.pop_back();
        }
    }
    vector<int> cnt(n);
    for(int i = 0; i < n; ++i){
        if(same[i] != -1){
            ++cnt[same[i]];
        }
    }
    int mn = (int)1e9;
    for(int i = 0; i < n; ++i){
        if(cnt[i] > 0) mn = min(mn, cnt[i]);
    }
    vector<int> ans(n, 0);
    for(int i = 0; i < n; ++i){
        if(same[i] != -1 && cnt[same[i]] == mn){
            ans[i] = 1;
        }
    }
    return ans;
}

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:66:29: warning: 'rt' may be used uninitialized in this function [-Wmaybe-uninitialized]
   66 |         for(auto&nxt:wall[rt]){
      |                             ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...