結果

問題 No.416 旅行会社
ユーザー ojisan_ITojisan_IT
提出日時 2017-10-11 01:42:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,936 bytes
コンパイル時間 1,366 ms
コンパイル使用メモリ 112,272 KB
実行使用メモリ 33,060 KB
最終ジャッジ日時 2023-08-10 22:34:47
合計ジャッジ時間 8,930 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
33,060 KB
testcase_01 AC 5 ms
8,652 KB
testcase_02 AC 5 ms
8,688 KB
testcase_03 AC 4 ms
8,760 KB
testcase_04 AC 4 ms
8,864 KB
testcase_05 AC 4 ms
8,628 KB
testcase_06 AC 4 ms
8,580 KB
testcase_07 AC 5 ms
8,676 KB
testcase_08 AC 9 ms
8,844 KB
testcase_09 AC 55 ms
9,476 KB
testcase_10 AC 165 ms
13,852 KB
testcase_11 AC 222 ms
23,116 KB
testcase_12 AC 235 ms
23,104 KB
testcase_13 AC 205 ms
23,116 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<tuple>
#include<string>
#include<cmath>
#include<climits>
#include<algorithm>
#include<bitset>
#include<set>
#include<stack>
using namespace std;  
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define rep(i,n) for(ll i=0;i<(n);i++)  
#define tii tuple<int,int>
#define tiii tuple<int,int,int>
#define mt make_tuple
#define pb push_back  
#define ALL(a) (a).begin(),(a).end()
#define FST first
#define SEC second  
const int INF = (INT_MAX/2);
const ll LLINF = (LLONG_MAX/2);
const double eps = 1e-5;
const double PI = M_PI;  
#define DEB cerr<<"!"<<endl
#define SHOW(a,b) cerr<<(a)<<" "<<(b)<<endl
#define SHOWARRAY(ar,i,j) REP(a,i)REP(b,j)cerr<<ar[a][b]<<((b==j-1)?((a==i-1)?("\n\n"):("\n")):(" "))
#define DIV 1000000007
ll pow(ll x,ll n,ll m){ll r=1;while(n>0){if((n&1)==1)r=r*x%m;x=x*x%m;n>>=1;}return r%m;}

int ans[100001] = {};
set<int> G[100001] = {};
#define Nodes 100001
class UF{
public:
  int value[Nodes];
  int over[Nodes]; // When over < 0, 'over' keep number of node(using negative number). -1 means this tree is a node.
  int root(int index){
    int t = index;
    stack<int> stack_i;
    while(over[t] >= 0){
      //stack_i.push(t);
      t = over[t];
    }
    while(!stack_i.empty()){
      int i = stack_i.top(); stack_i.pop();
      over[i] = t;
    }// reconnect
    return t;
  }
  void merge(int a,int b){
    if(this->root(a) == this->root(b)) return;
    over[root(a)] += over[root(b)]; // over have a number of nodes.
    over[root(b)] = root(a);
  }
  UF(){rep(i,Nodes) over[i] = -1;} // all nodes are root.
};

int n,m,q;
vector<tii> v;
UF u;
inline void bfs(int pos1,int pos2, int times){
  set<int> used;
  if(u.root(pos2) == u.root(1))
    swap(pos1,pos2);
  //SHOW(q-times,q);
  stack<int> si;
  si.push(pos2);
  used.insert(pos2);
  while(!si.empty()){
    pos2 = si.top(); si.pop();
    ans[pos2] = q-times+1;
    for(auto itr:G[pos2])
      if(used.find(itr) == used.end()){
        si.push(itr);
        used.insert(itr);
      }
  }
}

int main(){
  scanf("%d %d %d",&n,&m,&q);
  set<tii> input;
  rep(i,m){
    int a,b; scanf("%d %d",&a,&b);
    input.insert(tii{a,b});
  }
  stack<tii> que;
  rep(i,q){
    int c,d; scanf("%d %d",&c,&d);
    input.erase(tii{c,d});
    que.push(tii{c,d});
  }
  for(auto itr:input){
    int a,b;
    tie(a,b) = itr;
    if(u.root(a) != u.root(b)){
      G[a].insert(b);
      G[b].insert(a);
      u.merge(a,b);
    }
  }
  for(int i = 2; i <= n; i++)
    if(u.root(1) == u.root(i)) ans[i] = -1;
  int i = 1;
  while(!que.empty()){
    int c,d;
    tie(c,d) = que.top(); que.pop();
    if(u.root(c) == u.root(d))
      ;
    else if(u.root(1) == u.root(c) || u.root(1) == u.root(d)){
      bfs(c,d,i);
      u.merge(c,d);
    }else{
      G[c].insert(d);
      G[d].insert(c);
      u.merge(c,d);
    }
    i++;
  }
  rep(i,n-1)
    printf("%d\n",ans[i+2]);
}
0