結果

問題 No.416 旅行会社
ユーザー nanophoto12nanophoto12
提出日時 2016-09-18 21:21:01
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,790 bytes
コンパイル時間 1,322 ms
コンパイル使用メモリ 101,944 KB
実行使用メモリ 812,788 KB
最終ジャッジ日時 2024-04-28 15:39:42
合計ジャッジ時間 4,575 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 290 ms
113,784 KB
testcase_01 AC 46 ms
101,100 KB
testcase_02 AC 49 ms
100,976 KB
testcase_03 AC 47 ms
101,100 KB
testcase_04 AC 47 ms
101,104 KB
testcase_05 AC 47 ms
100,972 KB
testcase_06 AC 48 ms
100,972 KB
testcase_07 AC 47 ms
101,228 KB
testcase_08 AC 54 ms
101,100 KB
testcase_09 AC 77 ms
102,380 KB
testcase_10 AC 296 ms
113,784 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 <set>
#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)];
    int data[(int)(1e6+1)];
    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;
    }
};

vector<int> ad[(int)1e6+1];
set<int> rem[(int)1e6+1];
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[a].push_back(b);
    }
    REP(x,q)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        rem[a].insert(b);
        qs.push_back(make_pair(a,b));
    }
    UnionFind uf(n);
    
    vector<int> v(n);
    fill(v.begin(), v.end(), 0);
    for(int i = 0;i < n;i++)
    {
        for(int j = 0;j < ad[i].size();j++)
        {
            int k = ad[i][j];
            if(rem[i].find(k) == rem[i].end())
            {
                uf.unionSet(i, k, 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