Submission #1756119


Source Code Expand

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 3000000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define REP(i,n) for(long long i = 0;i < n;++i)                                                                             
#define seg_size 524288
vector<int> vertexs[200000];
int hukasa[200000] = {};
int findings[200000][100] = {};
int dfs(int now, int back,int deeply) {
	for (int i = 0;i < vertexs[now].size();++i) {
		if (vertexs[now][i] != back) {
			dfs(vertexs[now][i], now, deeply + 1);
		}
	}
	findings[now][1] = back;
	return hukasa[now] = deeply;
}
int dfsing(int now, int back,int deep) {
	int wa = (1 << (deep-1));
	if (wa <= hukasa[now]) {
		int ge = findings[now][deep - 1];
		ge = findings[ge][deep - 1];
		findings[now][deep] = ge;
	}
	for (int i = 0;i < vertexs[now].size();++i) {
		if (vertexs[now][i] != back) {
			dfsing(vertexs[now][i], now,deep);
		}
	}
	return 0;
}
int main() {
	int n;
	cin >> n;
	REP(i, n-1) {
		int a, b;
		cin >> a >> b;
		vertexs[a].push_back(b);
		vertexs[b].push_back(a);
	}
	dfs(1, -1, 0);
	for (int i = 2;i <= 18;++i) {
		dfsing(1, -1, i);
	}
	int query;
	cin >> query;
	for (int i = 0;i < query;++i) {
		int a, b;
		cin >> a >> b;
		if (hukasa[a] > hukasa[b]) swap(a, b);
		int ans = 0;
		while (hukasa[b] - hukasa[a] != 0) {
			int geko = hukasa[b] - hukasa[a];
			for (int i = 1;i >= 0;++i) {
				if ((1 << (i - 1)) > geko) {
					ans += 1 << (i - 2);
					b = findings[b][i - 1];
					break;
				}
			}
		}
		while (a != b) {
			if (findings[a][1] == findings[b][1]) {
				ans += 2;
				break;
			}
			int l = 1, r = 1;
			for (r = 1;r >= 0;++r) {
				if ((1 << r) > hukasa[a]) break;
			}
			r--;
			while (r - l > 1) {
				int m = (l + r) / 2;
				if (findings[a][m] == findings[b][m]) {
					r = m;
				}
				else {
					l = m;
				}
			}
			a = findings[a][l];
			b = findings[b][l];
			ans += 2 * (1 << (l - 1));
		}
		cout << ans+1 << endl;
	}
	return 0;
}

Submission Info

Submission Time
Task D - 閉路
User kotamanegi
Language C++14 (GCC 5.4.1)
Score 100
Code Size 2646 Byte
Status AC
Exec Time 451 ms
Memory 58624 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 30 / 30 70 / 70
Status
AC × 3
AC × 12
AC × 27
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 6016 KB
subtask0_sample02.txt AC 3 ms 6016 KB
subtask0_sample03.txt AC 3 ms 6016 KB
subtask1_01.txt AC 143 ms 57856 KB
subtask1_02.txt AC 145 ms 57856 KB
subtask1_03.txt AC 3 ms 6016 KB
subtask1_04.txt AC 3 ms 6016 KB
subtask1_05.txt AC 4 ms 6400 KB
subtask1_06.txt AC 4 ms 6528 KB
subtask1_07.txt AC 164 ms 50176 KB
subtask1_08.txt AC 169 ms 50176 KB
subtask1_09.txt AC 174 ms 50176 KB
subtask1_10.txt AC 175 ms 50176 KB
subtask1_11.txt AC 181 ms 50176 KB
subtask1_12.txt AC 175 ms 50176 KB
subtask2_01.txt AC 367 ms 58624 KB
subtask2_02.txt AC 369 ms 58496 KB
subtask2_03.txt AC 201 ms 6272 KB
subtask2_04.txt AC 213 ms 6272 KB
subtask2_05.txt AC 229 ms 6784 KB
subtask2_06.txt AC 231 ms 6784 KB
subtask2_07.txt AC 405 ms 50560 KB
subtask2_08.txt AC 424 ms 50432 KB
subtask2_09.txt AC 437 ms 50432 KB
subtask2_10.txt AC 439 ms 50560 KB
subtask2_11.txt AC 448 ms 50560 KB
subtask2_12.txt AC 451 ms 50560 KB