-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.cpp
191 lines (181 loc) · 5.35 KB
/
script.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <bits/stdc++.h>
using namespace std; //名前空間の宣言
using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::istringstream;
using std::ios;
using std::stringstream;
using std::chrono::duration_cast;
using namespace std::chrono;
using ll = long long; using ld = long double;
using ull = unsigned long long; using str = string;
using bl = bool; using ch = char;
using cd = complex<ld>;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<vector<ll>> vl2;
typedef vector<str> vs;
typedef map<ll, ll> mll;
#define LL_MAX 9223372036854775807
#define up(initial, n, step) for(ll i = (ll)(initial);i < (ll)(n);i+=(ll)(step))
#define up2(initial, n, step) for(ll j = (ll)(initial);j < (ll)(n);j+=(ll)(step))
#define up3(initial, n, step) for(ll k = (ll)(initial);k < (ll)(n);k+=(ll)(step))
#define down(initial, n, step) for(ll i = (ll)(initial) - 1;i >= (ll)(n);i-=(ll)(step))
#define stop return EXIT_SUCCESS;
#define toStr(s) to_string((s))
#define foreach(a, v) for(auto& a : v)
#define _m_ int main(void)
const str alphabet = "abcdefghijklmnopqrstuvwxyz";
const str upAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ld PHI = (1 + sqrt(5)) / 2;
const ll Mod = 998244353;
const ll Mod2 = pow(10, 9) + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1);
vl2 graph;
//O(M * LOG(N) + N)
ll maxNode(vl2 edges) {
ll len = edges.size();
ll n = graph.size();
mll m;
up(0, len, 1) {
// Storing the degree for each node
m[edges[i][0]]++;
m[edges[i][1]]++;
}
// maxi and mini variables to store
// the maximum and minimum degree
ll maxi = 0, mini = n;
up(1, n + 1, 1) {
maxi = max(maxi, m[i]);
mini = min(mini, m[i]);
}
up(1, n + 1, 1) {
if (m[i] == maxi)
return i;
}
};
//Check are 2
bl BFS(vl2& adj, ll a, ll b){
list<ll> queue;
ll v = graph.size();
vl visited(v, 0), dist(v, INT_MAX), pred(v, -1);
visited[a] = 1, dist[a] = 0, queue.push_back(a);
while (!queue.empty()) {
ll u = queue.front();
queue.pop_front();
up(0, (ll)adj[u].size(), 1) {
if (!visited[adj[u][i]]) {
visited[adj[u][i]] = 1;
dist[adj[u][i]] = dist[u] + 1;
pred[adj[u][i]] = u;
queue.push_back(adj[u][i]);
if (adj[u][i] == b) return 1;
}
}
}
return 0;
}
//Shortest Distance d(a, b)
ll dist(vl2 &adj, ll a, ll b) {
ll v = graph.size(), crawl = b;
vl pred(v), dist(v), path;
if (!BFS(adj, a, b)) return 0;
path.push_back(crawl);
while (pred[crawl] != -1) path.push_back(pred[crawl]), crawl = pred[crawl];
return dist[b];
}
//Middle Node
vl middle(ll a, ll b) {
ll v = graph.size(), crawl = b;
vl pred(v), dist(v), path;
path.push_back(crawl);
while (pred[crawl] != -1) path.push_back(pred[crawl]), crawl = pred[crawl];
return path;
}
//eccentricity of point a
ll ecc(vl2& adj, ll a) {
ll dt = 0;
up(1, (ll)graph.size(), 1) if(i != a) dt = max(dt, dist(adj, a, i));
return dt;
}
//Return lowerb and u
pair<ll, ll> sweep4(vl2 edges, ll r1) {
ll a1 = 0, a1Dist = 0;
up(1, (ll)graph.size(), 1) {
ll d = dist(edges, r1, i);
if (d > a1Dist)
a1Dist = d,
a1 = i;
}
ll b1 = 0, b1Dist = 0;
up(1, (ll)graph.size(), 1) {
ll d = dist(edges, a1, i);
if (d > b1Dist)
b1Dist = d,
b1 = i;
}
vl m = middle(a1, b1);
r1 = m[m.size() / 2];
ll a2 = a1;
a1 = 0, a1Dist = 0;
up(1, (ll)graph.size(), 1) {
ll d = dist(edges, r1, i);
if (d > a1Dist)
a1Dist = d,
a1 = i;
}
b1 = 0, b1Dist = 0;
up(1, (ll)graph.size(), 1) {
ll d = dist(edges, a1, i);
if (d > b1Dist)
b1Dist = d,
b1 = i;
}
vl m = middle(a1, b1);
ll u = m[m.size() / 2];
ll lowerb = max(ecc(edges, a1), ecc(edges, a2));
return { lowerb, u };
};
//In general, let F[i](u) be the fringe set of nodes at distance i from u (note that F(u) = F[ecc(u)](u)) and let
//B[i](u) = max[z∈Fi(u)] ecc(z) be the maximum eccentricity among these nodes
//
//Now, according to my understanding, B[i](u) is a function finding the max ecc and
//F[i](u) is a function finding all nodes v that d(u, v) = i. Correct me if I'm incorrect. I can't find any precise explanation online.
ll B(vl2& edges, ll i, ll u) {
//TO-DO
}
//OUTPUT: A value M such that D − M ≤ k
ll iFUB(vl2& edges, ll u, ll l, ll k) {
ll i = ecc(edges, u);
ll lb = max(i, l);
ll ub = 2 * i;
while (ub - lb > k) {
ll b = B(edges, i, u);
if (max(lb, b) > 2 * (i - 1)) {
return max(lb, b);
}
else {
lb = max(lb, b);
ub = 2 * (i - 1);
}
i--;
}
}
_m_{
std::ios::sync_with_stdio(EXIT_SUCCESS); std::cin.tie(EXIT_SUCCESS); std::cout.tie(EXIT_SUCCESS);
//Standard get input for graph stuff. Remember its undirected.
ll nodes = get(), edges = get();
graph.resize(nodes + 1); //graph[0] is empty
vl2 ed(edges, vl(2));
up(0, edges, 1) {
ed[i][0] = get(),
ed[i][1] = get(),
graph[ed[i][0]].push_back(ed[i][1]),
graph[ed[i][1]].push_back(ed[i][0]);
}
ll maxNd = maxNode(ed);
pair<ll, ll> sweep = sweep4(ed, maxNd);
cout << iFUB(ed, sweep.second, sweep.first, 0) << '\n';
stop
}