結果

問題 No.416 旅行会社
ユーザー pythontanukipythontanuki
提出日時 2023-01-04 17:23:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,538 bytes
コンパイル時間 4,084 ms
コンパイル使用メモリ 277,572 KB
実行使用メモリ 19,768 KB
最終ジャッジ日時 2023-08-18 14:59:05
合計ジャッジ時間 15,865 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using namespace std;
using mint = modint998244353;
using C = complex<double>;
const int mod = 998244353;
const long long LINF = 1001002003004005006;
const int INF = 1001001001;
const double PI = acos(-1);
const double EPS = 1e-10;
const int di[4] = {-1,0,1,0};
const int dj[4] = {0,-1,0,1};
const int dx[8] = {1,1,1,0,0,-1,-1,-1};
const int dy[8] = {1,0,-1,1,-1,1,0,-1};
# define sz(x) (int)(x).size()
# define rsz(x,n) x.resize(n)
# define yosupo(x) {cout << (x) << endl; return 0;}
# define ll long long
# define fi first
# define se second
# define pb push_back
# define pf push_front
# define eb emplace_back
# define ef emplace_front
# define pob pop_back
# define pof pop_front
# define GET_MACRO(_1, _2, _3, NAME, ...) NAME
# define _rep(i, n) _rep2(i, 0, n)
# define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
# define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
# define srep(i, a, b) for(int i = a; i <= b; ++i)
# define all(obj) (obj).begin(), (obj).end()
# define rall(obj) (obj).rbegin(), (obj).rend()
inline void YesNo(bool f) { std::cout << (f? "Yes": "No") << std::endl; }
void read() {}
template <typename T, class... U> void read(T &t, U &...u) { cin >> t; read(u...); }
void writeln() { cout << endl; }
template <typename T, class... U, char sep = ' '> void writeln(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; writeln(u...); }
# define Pll pair<ll, ll>
# define P pair<int,int>
# define bit(x,i) (((x) >> (i)) & 1)
# define equals(a, b) (fabs((a) - (b)) < EPS) // 誤差を考慮した同値判定
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}
template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { return os << "P(" << p.first << " " << p.second << ")";}
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
using vi = vc<int>; using vvi = vv<int>;
using vl = vc<ll>; using vvl = vv<ll>;
using vm = vc<mint>; using vvm = vv<mint>;

struct UnionFind {
    vector<int> par;
    vector<vector<int>> group;
    UnionFind() {}
    UnionFind(int n) : par(n, -1) , group(n){}
    void init(int n) {
        par.assign(n, -1);
        rep(i,n) group[i].pb(i);
    }
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    bool merge(int x, int y) {
        int px = root(x); int py = root(y);
        if (px == py) return false;
        if (par[px] > par[py]) swap(px, py); // merge technique
        group[px].insert(group[px].end(),group[py].begin(),group[py].end());
        group[py].clear();
        par[px] += par[py];
        par[py] = px;
        return true;
    }
    int size(int x) {
        return -par[root(x)];
    }
    vector<int> set(int i) {
        sort(all(group[root(i)]));
        return group[root(i)];
    }
};


int main() {
    int n,m,q;
    read(n,m,q);
    set<P> edge;
    rep(i,m) {
        int u,v;
        read(u,v);
        --u;--v;
        edge.insert(P(u,v));
    }
    vector<pair<int,int>> query;
    rep(i,q) {
        int u,v;
        read(u,v);
        --u;--v;
        edge.erase(P(u,v));
        query.push_back(make_pair(u,v));
    }
    reverse(all(query));
    UnionFind uf(n);
    uf.init(n);
    for(auto[u,v] : edge) uf.merge(u,v);
    vector<int> ans(n,0);
    rep(i,n) if(uf.same(0,i)) ans[i] = -1;
    rep(i,q) {
        auto[u,v] = query[i];
        bool u_before = uf.same(0,u);
        bool v_before = uf.same(0,v);
        uf.merge(u,v);
        bool u_after = uf.same(0,u);
        bool v_after = uf.same(0,v);
        if(!u_before && u_after) {
            for(int uu : uf.set(u)) if(ans[uu] == 0) ans[uu] = q-i;
        }
        if(!v_before && v_after) {
            for(int vv : uf.set(v)) if(ans[vv] == 0) ans[vv] = q-i;
        }
    }
    rep(i,1,n) cout << ans[i] << '\n';
}
















0