結果

問題 No.1326 ふたりのDominator
ユーザー chocoruskchocorusk
提出日時 2020-12-23 00:39:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,667 bytes
コンパイル時間 1,778 ms
コンパイル使用メモリ 148,948 KB
実行使用メモリ 34,368 KB
最終ジャッジ日時 2023-10-24 10:33:09
合計ジャッジ時間 9,306 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,696 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 5 ms
4,348 KB
testcase_08 AC 5 ms
4,348 KB
testcase_09 AC 5 ms
4,348 KB
testcase_10 AC 5 ms
4,348 KB
testcase_11 AC 5 ms
4,348 KB
testcase_12 AC 323 ms
20,856 KB
testcase_13 AC 332 ms
20,856 KB
testcase_14 AC 334 ms
20,868 KB
testcase_15 AC 352 ms
20,664 KB
testcase_16 AC 318 ms
20,156 KB
testcase_17 AC 289 ms
16,908 KB
testcase_18 AC 287 ms
14,296 KB
testcase_19 AC 249 ms
14,804 KB
testcase_20 AC 291 ms
29,104 KB
testcase_21 AC 325 ms
29,140 KB
testcase_22 TLE -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <stack>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

using G=vector<vector<int>>;
struct LowLink {
  const G &g;
  vector< int > used, ord, low;
  vector< int > articulation;
  vector< pair< int, int > > bridge;
 
  LowLink(const G &g) : g(g) {}
 
  int dfs(int idx, int k, int par) {
    used[idx] = true;
    ord[idx] = k++;
    low[idx] = ord[idx];
    bool is_articulation = false;
    int cnt = 0;
    for(auto &to : g[idx]) {
      if(!used[to]) {
        ++cnt;
        k = dfs(to, k, idx);
        low[idx] = min(low[idx], low[to]);
        is_articulation |= ~par && low[to] >= ord[idx];
        if(ord[idx] < low[to]) bridge.emplace_back(minmax(idx, (int) to));
      } else if(to != par) {
        low[idx] = min(low[idx], ord[to]);
      }
    }
    is_articulation |= par == -1 && cnt > 1;
    if(is_articulation) articulation.push_back(idx);
    return k;
  }
 
  virtual void build() {
    used.assign(g.size(), 0);
    ord.assign(g.size(), 0);
    low.assign(g.size(), 0);
    int k = 0;
    for(int i = 0; i < g.size(); i++) {
      if(!used[i]) k = dfs(i, k, -1);
    }
  }
};

template< typename G >
struct BiConnectedComponents : LowLink {
  using LL = LowLink;
 
  vector< int > used;
  vector< vector< pair< int, int > > > bc;
  vector< pair< int, int > > tmp;
 
  BiConnectedComponents(const G &g) : LL(g) {}
 
  void dfs(int idx, int par) {
    used[idx] = true;
    for(auto &to : this->g[idx]) {
      if(to == par) continue;
      if(!used[to] || this->ord[to] < this->ord[idx]) {
        tmp.emplace_back(minmax(idx, to));
      }
      if(!used[to]) {
        dfs(to, idx);
        if(this->low[to] >= this->ord[idx]) {
          bc.emplace_back();
          for(;;) {
            auto e = tmp.back();
            bc.back().emplace_back(e);
            tmp.pop_back();
            if(e.first == min(idx, to) && e.second == max(idx, to)) {
              break;
            }
          }
        }
      }
    }
  }
 
  void build() override {
    LL::build();
    used.assign(this->g.size(), 0);
    for(int i = 0; i < used.size(); i++) {
      if(!used[i]) dfs(i, -1);
    }
  }
};

struct DoublingLowestCommonAncestor {
  const int LOG;
  vector< int > dep;
  const G &g;
  vector< vector< int > > table;

  DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
    table.assign(LOG, vector< int >(g.size(), -1));
  }

  void dfs(int idx, int par, int d) {
    table[0][idx] = par;
    dep[idx] = d;
    for(auto &to : g[idx]) {
      if(to != par) dfs(to, idx, d + 1);
    }
  }

  void build() {
    dfs(0, -1, 0);
    for(int k = 0; k + 1 < LOG; k++) {
      for(int i = 0; i < table[k].size(); i++) {
        if(table[k][i] == -1) table[k + 1][i] = -1;
        else table[k + 1][i] = table[k][table[k][i]];
      }
    }
  }

  int query(int u, int v) {
    if(dep[u] > dep[v]) swap(u, v);
    for(int i = LOG - 1; i >= 0; i--) {
      if(((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
    }
    if(u == v) return u;
    for(int i = LOG - 1; i >= 0; i--) {
      if(table[i][u] != table[i][v]) {
        u = table[i][u];
        v = table[i][v];
      }
    }
    return table[0][u];
  }
};
int main()
{
	int n, m; cin>>n>>m;
    int u[50050], v[50050];
    G g(n);
    for(int i=0; i<m; i++){
        cin>>u[i]>>v[i];
        u[i]--; v[i]--;
        g[u[i]].push_back(v[i]);
        g[v[i]].push_back(u[i]);
    }
    BiConnectedComponents bcc(g);
    bcc.build();
    int b[50050];
    fill(b, b+n, -1);
    int n1=0;
    for(int x:bcc.articulation){
        b[x]=n1++;
    }
    n1=bcc.articulation.size()+bcc.bc.size();
    vector<vector<int>> g1(n1);
    for(int i=0; i<bcc.bc.size(); i++){
        auto v=bcc.bc[i];
        for(auto e:v){
            if(b[e.first]!=-1){
                int x=b[e.first], y=bcc.articulation.size()+i;
                g1[x].push_back(y);
                g1[y].push_back(x);
            }
            if(b[e.second]!=-1){
                int x=b[e.second], y=bcc.articulation.size()+i;
                g1[x].push_back(y);
                g1[y].push_back(x);
            }
        }
    }
    for(int i=0; i<bcc.bc.size(); i++){
        auto v=bcc.bc[i];
        for(auto e:v){
            if(b[e.first]==-1){
                b[e.first]=bcc.articulation.size()+i;
            }
            if(b[e.second]==-1){
                b[e.second]=bcc.articulation.size()+i;
            }
        }
    }
    DoublingLowestCommonAncestor lca(g1);
    lca.build();
    int d[200020];
    d[0]=1;
    auto dfs=[&](auto dfs, int x, int p)->void{
        for(auto y:g1[x]){
            if(y!=p){
                d[y]=d[x];
                if(y<bcc.articulation.size()) d[y]++;
                dfs(dfs, y, x);
            }
        }
    };
    dfs(dfs, 0, -1);
    int q; cin>>q;
    while(q--){
        int x, y; cin>>x>>y;
        x--; y--;
        int x1=b[x], y1=b[y];
        if(x1==y1){
            printf("0\n");
            continue;
        }
        int p1=lca.query(x1, y1);
        int ans=d[x1]+d[y1]-2*d[p1];
        if(p1<bcc.articulation.size()) ans++;
        if(x1<bcc.articulation.size()) ans--;
        if(y1<bcc.articulation.size()) ans--;
        printf("%d\n", ans);
    }
    return 0;
}
0