結果

問題 No.416 旅行会社
ユーザー nanophoto12nanophoto12
提出日時 2016-09-18 21:11:59
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,695 bytes
コンパイル時間 1,162 ms
コンパイル使用メモリ 114,016 KB
実行使用メモリ 812,804 KB
最終ジャッジ日時 2024-04-28 15:39:30
合計ジャッジ時間 5,053 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 349 ms
45,316 KB
testcase_01 AC 20 ms
26,880 KB
testcase_02 AC 18 ms
26,752 KB
testcase_03 AC 19 ms
26,752 KB
testcase_04 AC 20 ms
26,752 KB
testcase_05 AC 19 ms
26,752 KB
testcase_06 AC 20 ms
26,752 KB
testcase_07 AC 22 ms
26,880 KB
testcase_08 AC 26 ms
27,136 KB
testcase_09 AC 58 ms
28,744 KB
testcase_10 AC 360 ms
45,320 KB
testcase_11 MLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

//
//  main.cpp
//  No416
//
//  Created by nanophoto on 2016/09/18.
//  Copyright © 2016年 nanophoto. All rights reserved.
//

#include <cmath>
#include <vector>
#include <map>
#include <functional>
#include <queue>
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cstdint>
#include <climits>
#include <unordered_set>

using namespace std;

#define ll long long int
#define ti4 tuple<int,int,int,int>
#define pii pair<int,int>
#define REP(x,n) for(int x = 0;x < n;x++)

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

struct UnionFind {
    vector<int> m[(int)(1e6+1)];
    map<int, int> data;
    UnionFind(int size)
    {
        for(int i = 0;i < size;i++)
        {
            data[i] = i;
            m[i].push_back(i);
        }
    }
    bool unionSet(int x, int y, vector<int>& v, int times) {
        x = root(x); y = root(y);
        if (x != y) {
            int l = min(x, y);
            int u = max(x, y);
            for(auto& e : m[u])
            {
                m[l].push_back(e);
                if(l == 0)
                {
                    v[e] = times;
                }
            }
            data[u] = l;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        if(data[x] == x)
        {
            return x;
        }
        data[x] = root(data[x]);
        return data[x];
    }
};

const ll mod = (ll)(1e6+7);

struct SimpleHash {
    size_t operator()(const std::pair<int, int>& p) const {
        return p.first * mod + p.second;
    }
};

unordered_set<pii, SimpleHash> ad;
unordered_set<pii, SimpleHash> rem;
vector<pii> qs;

int main() {
    int n, m, q;
    cin >> n >> m >> q;
    REP(x,m)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        ad.insert(make_pair(a,b));
    }
    REP(x,q)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        rem.insert(make_pair(a,b));
        qs.push_back(make_pair(a,b));
    }
    UnionFind uf(n);
    
    vector<int> v(n);
    fill(v.begin(), v.end(), 0);
    for(auto& e : ad)
    {
        if(rem.find(e) == rem.end())
        {
            uf.unionSet(e.first, e.second, v, -1);
        }
    }
    for(int i = q-1; i >= 0;i--)
    {
        uf.unionSet(qs[i].first, qs[i].second, v, i + 1);
        //for(int k = 0;k < n;k++)
        //{
        //    cout << " " << k << " " << uf.root(k) << endl;
        //}
        //cout << endl;
    }
    for(int i = 1;i < n;i++)
    {
        cout << v[i] << endl;
    }
}
0