#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using T = tuple<ll, ll, ll>;
#define int long long
#define Base 41
#define sz(a) (int)a.size()
#define FOR(i, a, b) for ( int i = a ; i <= b ; i++ )
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)
#define all(x) x.begin() , x.end()
#define pii pair<int , int>
#define fi first
#define se second
#define Lg(x) 31 - __builtin_clz(x)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int MAX = 2e5 + 5;
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void setupIO(){
#define name "Whisper"
//Phu Trong from Nguyen Tat Thanh High School for gifted student
srand(time(NULL));
cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
cout << fixed << setprecision(10);
}
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
int numNode, numQuery, nDay;
vector<pii> adj[MAX];
int par[MAX], sz[MAX];
int find(int u){
return u == par[u] ? u : par[u] = find(par[u]);
}
bool unite(int u, int v){
if ((u = find(u)) == (v = find(v))) return false;
if (sz[u] < sz[v]) swap(u, v);
sz[u] += sz[v];
par[v] = u;
return true;
}
void addEdge(int id){
int g = nDay - id + 1;
for (int i = 2 * g; i <= numNode; i += g){
if (unite(i, g)){
adj[i].push_back({g, id});
adj[g].push_back({i, id});
}
}
}
int dep[MAX];
int up[MAX][22], mx[MAX][22];
void dfs(int u, int p){
for (auto&[v, w] : adj[u]) if(v ^ p){
dep[v] = dep[u] + 1;
mx[v][0] = w; up[v][0] = u;
for (int i = 1; i <= 21; ++i){
up[v][i] = up[up[v][i - 1]][i - 1];
mx[v][i] = max(mx[v][i - 1], mx[up[v][i - 1]][i - 1]);
}
dfs(v, u);
}
}
int query(int u, int v){
int ans = 0;
if (dep[u] < dep[v]) swap(u, v);
int dis = dep[u] - dep[v];
for (int i = 0; MASK(i) <= dis; ++i){
if (BIT(dis, i)) maximize(ans, mx[u][i]), u = up[u][i];
}
if (u == v) return ans;
for (int i = 21; i >= 0; --i) if(up[u][i] != up[v][i]){
maximize(ans, mx[v][i]);
maximize(ans, mx[u][i]);
u = up[u][i], v = up[v][i];
}
maximize(ans, mx[v][0]);
maximize(ans, mx[u][0]);
return ans;
}
void Whisper(){
// for each query find the minimum day that two citys u, v are in the same connected component
cin >> numNode >> nDay >> numQuery;
for (int i = 1; i <= numNode; ++i) par[i] = i, sz[i] = 1;
for (int i = 1; i <= nDay; ++i) addEdge(i);
dfs(1, 0);
for (int i = 1; i <= numQuery; ++i){
int u, v; cin >> u >> v;
cout << query(u, v) << '\n';
}
}
signed main(){
setupIO();
int Test = 1;
// cin >> Test;
for ( int i = 1 ; i <= Test ; i++ ){
Whisper();
if (i < Test) cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
11664 KB |
Output is correct |
2 |
Correct |
18 ms |
11868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
12116 KB |
Output is correct |
2 |
Correct |
31 ms |
12176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
21340 KB |
Output is correct |
2 |
Correct |
25 ms |
21204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
25884 KB |
Output is correct |
2 |
Correct |
39 ms |
26196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
31332 KB |
Output is correct |
2 |
Correct |
43 ms |
33412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
38580 KB |
Output is correct |
2 |
Correct |
88 ms |
41040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
45924 KB |
Output is correct |
2 |
Correct |
95 ms |
50564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
55644 KB |
Output is correct |
2 |
Correct |
109 ms |
55556 KB |
Output is correct |