Все задачи на 5.10.17 13:00

This commit is contained in:
2017-10-05 13:08:02 +03:00
parent 93746b05d7
commit ca2ca71f63
120 changed files with 2343 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6;
vector<int> tree[MAXN];
int lel[MAXN];
int in[MAXN];
int out[MAXN];
int timer = 0;
bool is_anc(int a, int b) {
return (in[a] <= in[b]) && (out[b] <= out[a]);
}
void dfs(int v, int p, int h) {
lel[v] = h;
in[v] = timer++;
for (int u : tree[v]) {
if (u == p) {
continue;
}
dfs(u, v, h + 1);
}
out[v] = timer++;
}
int main() {
//assert(freopen("../4/A.txt", "r", stdin));
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i < n; i++) {
int v;
cin >> v;
tree[i].push_back(--v);
tree[v].push_back(i);
}
dfs(0, -1, 0);
for (int i = 0; i < m; i++) {
int vi, ui;
cin >> vi >> ui;
--vi;
--ui;
if (!is_anc(vi, ui)){
cout << "no\n";
continue;
}
if (lel[ui] - lel[vi] > k) {
cout << "no\n";
} else {
cout << "yes\n";
}
}
}

View File

@@ -0,0 +1,6 @@
5 4 3
1 1 1 4
3 3
1 3
1 3
2 4

View File

@@ -0,0 +1,71 @@
//
// Created by vlad on 29.06.17.
//
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
const int MAXN = 1e6;
const int MAXLOG = 25;
vector<int> tree[MAXN];
int lel[MAXN];
int in[MAXN];
int out[MAXN];
int timer = 0;
int up[MAXN][MAXLOG];
bool is_anc(int a, int b) {
return (in[a] <= in[b]) && (out[b] <= out[a]);
}
void add(int a, int p) {
up[a][0] = p;
for (int i = 1; i < MAXLOG; i++) up[a][i] = up[up[a][i - 1]][i - 1];
}
void dfs(int v, int p, int h) {
lel[v] = h;
in[v] = timer++;
for (int u : tree[v]) {
if (u == p) continue;
add(u, v);
dfs(u, v, h + 1);
}
out[v] = timer++;
}
int lca(int a, int b){
if (is_anc(a, b)) return a;
if (is_anc(b, a)) return b;
for(int i = MAXLOG-1; i>=0; --i){
int v = up[a][i];
if (!is_anc(v, b)) a = v;
}
return up[a][0];
}
int main() {
//assert(freopen("../4/B.txt", "r", stdin));
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n;
for (int i = 1; i < n; i++) {
int pi, qi;
cin >> pi >> qi;
tree[--pi].push_back(--qi);
tree[qi].push_back(pi);
}
cin >> m;
dfs(0, -1, 0);
for (int i = 0; i < m; i++) {
int ai, bi;
cin >> ai >> bi;
ai--;
bi--;
cout << lca(ai, bi) + 1 << endl;
}
return 0;
}

View File

@@ -0,0 +1,11 @@
6
6 3
5 6
2 1
2 4
4 5
4
6 1
2 4
4 4
5 3

Binary file not shown.

View File

@@ -0,0 +1,83 @@
//
// Created by vlad on 29.06.17.
//
#include <cstdio>
/** Interface */
inline int readChar();
template <class T = int> inline T readInt();
template <class T> inline void writeInt( T x, char end = 0 );
inline void writeChar( int x );
inline void writeWord( const char *s );
/** Read */
static const int buf_size = 4096;
inline int getChar() {
static char buf[buf_size];
static int len = 0, pos = 0;
if (pos == len)
pos = 0, len = fread(buf, 1, buf_size, stdin);
if (pos == len)
return -1;
return buf[pos++];
}
inline int readChar() {
int c = getChar();
while (c <= 32)
c = getChar();
return c;
}
template <class T>
inline T readInt() {
int s = 1, c = readChar();
T x = 0;
if (c == '-')
s = -1, c = getChar();
while ('0' <= c && c <= '9')
x = x * 10 + c - '0', c = getChar();
return s == 1 ? x : -x;
}
/** Write */
static int write_pos = 0;
static char write_buf[buf_size];
inline void writeChar( int x ) {
if (write_pos == buf_size)
fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
write_buf[write_pos++] = x;
}
template <class T>
inline void writeInt( T x, char end ) {
if (x < 0)
writeChar('-'), x = -x;
char s[24];
int n = 0;
while (x || !n)
s[n++] = '0' + x % 10, x /= 10;
while (n--)
writeChar(s[n]);
if (end)
writeChar(end);
}
inline void writeWord( const char *s ) {
while (*s)
writeChar(*s++);
}
struct Flusher {
~Flusher() {
if (write_pos)
fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
}
} flusher;