Submission #2197150


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

//#define int long long
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define rept(i,n) reps(i,0,((n)+1))
#define repst(i,s,n) reps(i,s,((n)+1))
#define reprt(i,n,t) for(int (i)=(n);(i)>=(t);--(i))
#define repr(i,n) reprt(i,n,0)
#define each(itr,v) for(auto &(itr):(v))
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define mp(x,y) make_pair((x),(y))
#define fi first
#define se second
#define tmax(x,y,z) max(x,max(y,z))
#define tmin(x,y,z) min(x,min(y,z))
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define ln '\n'
#define bln(i,n) (i==n-1?'\n':' ')
#define dbg(x) cout<<#x" = "<<(x)<<ln
#define dbga(x,n) {cout<<#x" : ";for(int (i)=0;i<(n);++i){cout<<((x)[i])<<(i==((n)-1)?'\n':' ');}}

typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vst;
typedef vector<bool> vb;
typedef vector<ld> vld;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vector<int> > mat;

const ll inf = (ll)1e9+10;
const ll linf = (ll)1e18+10;
const ll mod = (ll)(1e9+7);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};
const double eps = 1e-10;

struct oreno_initializer {
	oreno_initializer() {
		cin.tie(0);
		ios::sync_with_stdio(0);
	}
} oreno_initializer;

// ━━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…
// .。.:( ^ω^)・゚+.。.:( ^ω^)・゚+.。.:( ^ω^)・゚+.。.:( ^ω^)・゚+.。.:( ^ω^)・゚+
// ・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・

const int MAX_V = 100100;
int n, a, b, q, d[MAX_V];
vi G[MAX_V];

int root;				// 根ノードの番号
int parent[20][MAX_V];		// parent[k][i]: iの親を2^k回辿って到達する頂点 (根を通り過ぎる場合は-1)
int depth[MAX_V];		// 根からの深さ

void LCAdfs(int v, int p, int d) {
	parent[0][v] = p;
	depth[v] = d;
	for (int i = 0; i < G[v].size(); ++i) {
		if (G[v][i] != p) LCAdfs(G[v][i], v, d + 1);
	}
}

// LCAの初期化
void LCAinit(int V) {
	// parent[0](真上の親)とdepthを初期化する
	LCAdfs(root, -1, 0);
	// parent[k](k>=1)を初期化する
	for (int k = 0; k+1 < log(MAX_V); ++k) {
		for (int v = 0; v < V; ++v) {
			if (parent[k][v] < 0) parent[k+1][v] = -1;
			else parent[k+1][v] = parent[k][parent[k][v]];
		}
	}
}

// 2頂点のLCAを求める
int LCA(int u, int v) {
	// uとvの深さが同じになるまで親を辿る
	if (depth[u] > depth[v]) swap(u, v);
	for (int k = 0; k < log(MAX_V); ++k) {
		if ((depth[v] - depth[u])>>k & 1) {
			v = parent[k][v];
		}
	}
	if (u == v) return u;
	
	// 二分探索でLCAを求める
	for (int k = (int)log(MAX_V)+1; k >= 0; --k) {
		if (parent[k][u] != parent[k][v]) {
			u = parent[k][u];
			v = parent[k][v];
		}
	}
	return parent[0][u];
}

void dijkstra(int s, vector<int> g[], int *d) {
	priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> que;
	fill(d, d+n, inf);
	d[s] = 0;
	que.push(make_pair(0,s));
	
	while(!que.empty()) {
		pair<int,int> p = que.top(); que.pop();
		int v = p.second, dis = p.first;
		if (d[v]<dis) continue;
		for (int i = 0; i < g[v].size(); ++i) {
			int to = g[v][i], cost = 1;
			if (d[to] > d[v] + cost) {
				d[to] = d[v] + cost;
				que.push(make_pair(d[to], to));
			}
		}
	}
}

signed main() {
	cin >> n;
	rep(i,n-1) {
		cin >> a >> b;
		a--, b--;
		G[a].pb(b), G[b].pb(a);
	}
	LCAinit(n);
	dijkstra(0,G,d);
	cin >> q;
	while (q--) {
		cin >> a >> b;
		a--, b--;
		int p = LCA(a,b);
		cout << d[a]+d[b]-2*d[p]+1 << ln;
	}
}

Submission Info

Submission Time
Task D - 閉路
User creep04
Language C++14 (GCC 5.4.1)
Score 0
Code Size 4122 Byte
Status WA
Exec Time 118 ms
Memory 19200 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 0 / 30 0 / 70
Status
AC × 3
AC × 11
WA × 1
AC × 25
WA × 2
Set Name Test Cases
Sample subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
Subtask1 subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt
Subtask2 subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt
Case Name Status Exec Time Memory
subtask0_sample01.txt AC 3 ms 6784 KB
subtask0_sample02.txt AC 3 ms 6784 KB
subtask0_sample03.txt AC 3 ms 6784 KB
subtask1_01.txt AC 36 ms 18560 KB
subtask1_02.txt WA 36 ms 18560 KB
subtask1_03.txt AC 3 ms 6784 KB
subtask1_04.txt AC 3 ms 6784 KB
subtask1_05.txt AC 3 ms 6784 KB
subtask1_06.txt AC 3 ms 6784 KB
subtask1_07.txt AC 57 ms 12672 KB
subtask1_08.txt AC 60 ms 12544 KB
subtask1_09.txt AC 64 ms 12416 KB
subtask1_10.txt AC 64 ms 12416 KB
subtask1_11.txt AC 59 ms 12416 KB
subtask1_12.txt AC 56 ms 12416 KB
subtask2_01.txt AC 62 ms 19200 KB
subtask2_02.txt WA 62 ms 19200 KB
subtask2_03.txt AC 26 ms 6912 KB
subtask2_04.txt AC 29 ms 7040 KB
subtask2_05.txt AC 34 ms 7168 KB
subtask2_06.txt AC 34 ms 7168 KB
subtask2_07.txt AC 108 ms 12832 KB
subtask2_08.txt AC 114 ms 12732 KB
subtask2_09.txt AC 111 ms 12672 KB
subtask2_10.txt AC 114 ms 12800 KB
subtask2_11.txt AC 118 ms 12800 KB
subtask2_12.txt AC 117 ms 12800 KB