#include "simurgh.h"
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
#define query count_common_roads
const int maxN = 500;
struct Jmp
{
int to, ind;
operator int&() { return to; }
operator const int&() const { return to; }
};
int n;
int in[maxN], out[maxN];
Jmp par[maxN];
int gold[maxN * maxN];
vector<int> dfsq;
vector<Jmp> nei[maxN];
vector<int> same[maxN * maxN];
Jmp fup[maxN];
map<vector<int>, int> cache;
int Query(vector<int> q)
{
sort(q.begin(), q.end());
auto it = cache.find(q);
if (it != cache.end()) return it->second;
return cache[q] = query(q);
}
inline bool Upper(int u, int v) { return in[u] <= in[v] && out[u] >= out[v]; }
bool used[maxN];
void DFS(int v, Jmp p)
{
// cerr << "v: " << v << '\n';
static int time = 0;
used[v] = true;
in[v] = ++time;
par[v] = p;
fup[v] = {-1, -1};
for (Jmp to: nei[v])
if (!used[to])
{
dfsq.push_back(to.ind);
DFS(to, {v, to.ind});
if (p != -1)
if (fup[v] == -1 || (fup[to] != -1 && in[fup[to]] <= in[fup[v]]))
fup[v] = fup[to];
}
else if (to != p && p != -1)
if (fup[v] == -1 || in[to] <= in[fup[v]])
fup[v] = to;
out[v] = time;
}
void DFS1(int v)
{
for (int to: same[v])
if (gold[to] == -1)
{
gold[to] = gold[v];
DFS1(to);
}
}
int pr[maxN * maxN], sz[maxN * maxN], ok[maxN * maxN];
void MakeSet(int i) { pr[i] = i; sz[i] = 1; }
int Set(int i) { return pr[i] == i ? i : pr[i] = Set(pr[i]); }
void Union(int i, int j)
{
i = Set(i), j = Set(j);
if (i == j) return;
if (sz[i] > sz[j]) swap(i, j);
sz[j] += sz[i];
ok[j] = max(ok[j], ok[i]);
pr[i] = j;
}
namespace Solve1
{
int edge[maxN][maxN];
bool areadj[maxN][maxN];
int Count(int s, int l, int r)
{
int ans = 0;
vector<int> q;
for (int i = l; i <= r; ++i)
q.push_back(edge[s][i]);
for (int i = 0; i < n - 1; ++i)
if ((i < l || r < i) && i != s)
{
q.push_back(edge[n - 1][i]);
ans -= areadj[n - 1][i];
}
q.push_back(edge[n - 1][l]);
ans -= areadj[n - 1][l];
return ans += Query(q);
}
void Add(int s, int l, int r)
{
int cnt = Count(s, l, r);
if (cnt == 0) return;
if (cnt == r - l + 1)
{
for (int i = l; i <= r; ++i)
areadj[s][i] = areadj[i][s] = true;
return;
}
int m = l + (r - l) / 2;
Add(s, l, m);
Add(s, m + 1, r);
}
vector<int> find_roads(int n_, vector<int> us, vector<int> vs)
{
n = n_;
for (int i = 0; i < us.size(); ++i)
edge[us[i]][vs[i]] = edge[vs[i]][us[i]] = i;
vector<bool> is(n - 1);
vector<int> q;
for (int i = 0; i + 1 < n - 1; ++i)
q.push_back(edge[i][i + 1]);
vector<int> qans(n - 1);
for (int i = 0; i < n - 1; ++i)
{
q.push_back(edge[n - 1][i]);
qans[i] = Query(q);
q.pop_back();
}
int mx = *max_element(qans.begin(), qans.end());
for (int i = 0; i < n - 1; ++i)
if (qans[i] == mx)
areadj[i][n - 1] = areadj[n - 1][i] = true;
// Find edges adj. to n - 1
for (int v = 1; v < n - 1; ++v)
Add(v, 0, v - 1);
vector<int> res;
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
if (areadj[i][j])
res.push_back(edge[i][j]);
return res;
}
}
vector<int> find_roads(int n_, vector<int> us, vector<int> vs)
{
n = n_;
if (us.size() == (n * (n - 1)) / 2)
return Solve1::find_roads(n, us, vs);
int m = us.size();
fill(gold, gold + m, -1);
for (int i = 0; i < m; ++i)
{
MakeSet(i);
nei[us[i]].push_back({vs[i], i});
nei[vs[i]].push_back({us[i], i});
}
DFS(0, {-1, -1});
// for (int i = 0; i < n; ++i)
// cerr << "fup[" << i << "] = " << fup[i].to << '\n';
int dfsans = Query(dfsq);
for (int i = 0; i < m; ++i)
if (in[us[i]] > in[vs[i]])
swap(us[i], vs[i]);
for (int i = 0; i < m; ++i)
{
int u = us[i], v = vs[i];
if (fup[v] == -1 || in[fup[v]] > in[u]) // Is a birdge.
{
gold[i] = 1;
continue;
}
if (par[v] == u) continue;
while (true)
{
int backe = i, dfse = par[v].ind;
if (Set(backe) == Set(dfse))
{}
else if (ok[Set(backe)] && ok[Set(dfse)])
{}
else
{
// cerr << dfse << ' ' << backe << '\n';
int j = find(dfsq.begin(), dfsq.end(), dfse) - dfsq.begin();
dfsq[j] = backe;
int curans = Query(dfsq);
if (curans == dfsans)
{
same[backe].push_back(dfse);
same[dfse].push_back(backe);
Union(dfse, backe);
}
else if (curans > dfsans)
{
gold[backe] = 1, gold[dfse] = 0;
ok[Set(backe)] = ok[Set(dfse)] = 1;
}
else
{
gold[backe] = 0, gold[dfse] = 1;
ok[Set(backe)] = ok[Set(dfse)] = 1;
}
dfsq[j] = dfse;
}
if ((v = par[v]) == u) break;
}
}
for (int i = 0; i < m; ++i)
if (gold[i] != -1)
DFS1(i);
vector<int> ans;
for (int i = 0; i < m; ++i)
if (gold[i] == 1)
ans.push_back(i);
return ans;
}
Compilation message
simurgh.cpp: In function 'std::vector<int> Solve1::find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:116:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < us.size(); ++i)
~~^~~~~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:147:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (us.size() == (n * (n - 1)) / 2)
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
correct |
2 |
Correct |
8 ms |
6272 KB |
correct |
3 |
Correct |
8 ms |
6272 KB |
correct |
4 |
Correct |
8 ms |
6272 KB |
correct |
5 |
Correct |
8 ms |
6272 KB |
correct |
6 |
Correct |
8 ms |
6272 KB |
correct |
7 |
Correct |
8 ms |
6272 KB |
correct |
8 |
Correct |
8 ms |
6272 KB |
correct |
9 |
Correct |
8 ms |
6272 KB |
correct |
10 |
Correct |
8 ms |
6272 KB |
correct |
11 |
Correct |
8 ms |
6272 KB |
correct |
12 |
Correct |
8 ms |
6272 KB |
correct |
13 |
Correct |
8 ms |
6272 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
correct |
2 |
Correct |
8 ms |
6272 KB |
correct |
3 |
Correct |
8 ms |
6272 KB |
correct |
4 |
Correct |
8 ms |
6272 KB |
correct |
5 |
Correct |
8 ms |
6272 KB |
correct |
6 |
Correct |
8 ms |
6272 KB |
correct |
7 |
Correct |
8 ms |
6272 KB |
correct |
8 |
Correct |
8 ms |
6272 KB |
correct |
9 |
Correct |
8 ms |
6272 KB |
correct |
10 |
Correct |
8 ms |
6272 KB |
correct |
11 |
Correct |
8 ms |
6272 KB |
correct |
12 |
Correct |
8 ms |
6272 KB |
correct |
13 |
Correct |
8 ms |
6272 KB |
correct |
14 |
Correct |
11 ms |
6528 KB |
correct |
15 |
Correct |
11 ms |
6524 KB |
correct |
16 |
Correct |
9 ms |
6400 KB |
correct |
17 |
Correct |
12 ms |
6656 KB |
correct |
18 |
Correct |
9 ms |
6400 KB |
correct |
19 |
Correct |
12 ms |
6656 KB |
correct |
20 |
Correct |
12 ms |
6656 KB |
correct |
21 |
Correct |
12 ms |
6656 KB |
correct |
22 |
Correct |
11 ms |
6528 KB |
correct |
23 |
Correct |
11 ms |
6400 KB |
correct |
24 |
Correct |
10 ms |
6400 KB |
correct |
25 |
Correct |
8 ms |
6320 KB |
correct |
26 |
Correct |
10 ms |
6400 KB |
correct |
27 |
Correct |
10 ms |
6400 KB |
correct |
28 |
Correct |
9 ms |
6400 KB |
correct |
29 |
Correct |
8 ms |
6272 KB |
correct |
30 |
Correct |
11 ms |
6432 KB |
correct |
31 |
Correct |
11 ms |
6400 KB |
correct |
32 |
Correct |
10 ms |
6400 KB |
correct |
33 |
Correct |
11 ms |
6528 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
correct |
2 |
Correct |
8 ms |
6272 KB |
correct |
3 |
Correct |
8 ms |
6272 KB |
correct |
4 |
Correct |
8 ms |
6272 KB |
correct |
5 |
Correct |
8 ms |
6272 KB |
correct |
6 |
Correct |
8 ms |
6272 KB |
correct |
7 |
Correct |
8 ms |
6272 KB |
correct |
8 |
Correct |
8 ms |
6272 KB |
correct |
9 |
Correct |
8 ms |
6272 KB |
correct |
10 |
Correct |
8 ms |
6272 KB |
correct |
11 |
Correct |
8 ms |
6272 KB |
correct |
12 |
Correct |
8 ms |
6272 KB |
correct |
13 |
Correct |
8 ms |
6272 KB |
correct |
14 |
Correct |
11 ms |
6528 KB |
correct |
15 |
Correct |
11 ms |
6524 KB |
correct |
16 |
Correct |
9 ms |
6400 KB |
correct |
17 |
Correct |
12 ms |
6656 KB |
correct |
18 |
Correct |
9 ms |
6400 KB |
correct |
19 |
Correct |
12 ms |
6656 KB |
correct |
20 |
Correct |
12 ms |
6656 KB |
correct |
21 |
Correct |
12 ms |
6656 KB |
correct |
22 |
Correct |
11 ms |
6528 KB |
correct |
23 |
Correct |
11 ms |
6400 KB |
correct |
24 |
Correct |
10 ms |
6400 KB |
correct |
25 |
Correct |
8 ms |
6320 KB |
correct |
26 |
Correct |
10 ms |
6400 KB |
correct |
27 |
Correct |
10 ms |
6400 KB |
correct |
28 |
Correct |
9 ms |
6400 KB |
correct |
29 |
Correct |
8 ms |
6272 KB |
correct |
30 |
Correct |
11 ms |
6432 KB |
correct |
31 |
Correct |
11 ms |
6400 KB |
correct |
32 |
Correct |
10 ms |
6400 KB |
correct |
33 |
Correct |
11 ms |
6528 KB |
correct |
34 |
Correct |
88 ms |
11256 KB |
correct |
35 |
Correct |
650 ms |
37500 KB |
correct |
36 |
Correct |
443 ms |
27896 KB |
correct |
37 |
Correct |
29 ms |
7544 KB |
correct |
38 |
Correct |
660 ms |
38396 KB |
correct |
39 |
Correct |
599 ms |
33656 KB |
correct |
40 |
Correct |
432 ms |
27896 KB |
correct |
41 |
Correct |
32 ms |
8568 KB |
correct |
42 |
Correct |
680 ms |
38496 KB |
correct |
43 |
Correct |
337 ms |
23160 KB |
correct |
44 |
Correct |
278 ms |
19960 KB |
correct |
45 |
Correct |
340 ms |
22008 KB |
correct |
46 |
Correct |
225 ms |
18168 KB |
correct |
47 |
Correct |
98 ms |
11512 KB |
correct |
48 |
Correct |
13 ms |
6528 KB |
correct |
49 |
Correct |
30 ms |
7672 KB |
correct |
50 |
Correct |
102 ms |
11384 KB |
correct |
51 |
Correct |
324 ms |
22136 KB |
correct |
52 |
Correct |
280 ms |
19704 KB |
correct |
53 |
Correct |
230 ms |
18168 KB |
correct |
54 |
Correct |
365 ms |
23288 KB |
correct |
55 |
Correct |
308 ms |
22136 KB |
correct |
56 |
Correct |
332 ms |
22136 KB |
correct |
57 |
Correct |
314 ms |
22148 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
6272 KB |
correct |
2 |
Correct |
9 ms |
6272 KB |
correct |
3 |
Correct |
287 ms |
20512 KB |
correct |
4 |
Correct |
456 ms |
28664 KB |
correct |
5 |
Correct |
481 ms |
29064 KB |
correct |
6 |
Correct |
173 ms |
16780 KB |
correct |
7 |
Correct |
354 ms |
24568 KB |
correct |
8 |
Correct |
348 ms |
23544 KB |
correct |
9 |
Correct |
424 ms |
28024 KB |
correct |
10 |
Correct |
455 ms |
29144 KB |
correct |
11 |
Correct |
415 ms |
27668 KB |
correct |
12 |
Correct |
456 ms |
29048 KB |
correct |
13 |
Correct |
8 ms |
6272 KB |
correct |
14 |
Correct |
367 ms |
25080 KB |
correct |
15 |
Correct |
448 ms |
29024 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
correct |
2 |
Correct |
8 ms |
6272 KB |
correct |
3 |
Correct |
8 ms |
6272 KB |
correct |
4 |
Correct |
8 ms |
6272 KB |
correct |
5 |
Correct |
8 ms |
6272 KB |
correct |
6 |
Correct |
8 ms |
6272 KB |
correct |
7 |
Correct |
8 ms |
6272 KB |
correct |
8 |
Correct |
8 ms |
6272 KB |
correct |
9 |
Correct |
8 ms |
6272 KB |
correct |
10 |
Correct |
8 ms |
6272 KB |
correct |
11 |
Correct |
8 ms |
6272 KB |
correct |
12 |
Correct |
8 ms |
6272 KB |
correct |
13 |
Correct |
8 ms |
6272 KB |
correct |
14 |
Correct |
11 ms |
6528 KB |
correct |
15 |
Correct |
11 ms |
6524 KB |
correct |
16 |
Correct |
9 ms |
6400 KB |
correct |
17 |
Correct |
12 ms |
6656 KB |
correct |
18 |
Correct |
9 ms |
6400 KB |
correct |
19 |
Correct |
12 ms |
6656 KB |
correct |
20 |
Correct |
12 ms |
6656 KB |
correct |
21 |
Correct |
12 ms |
6656 KB |
correct |
22 |
Correct |
11 ms |
6528 KB |
correct |
23 |
Correct |
11 ms |
6400 KB |
correct |
24 |
Correct |
10 ms |
6400 KB |
correct |
25 |
Correct |
8 ms |
6320 KB |
correct |
26 |
Correct |
10 ms |
6400 KB |
correct |
27 |
Correct |
10 ms |
6400 KB |
correct |
28 |
Correct |
9 ms |
6400 KB |
correct |
29 |
Correct |
8 ms |
6272 KB |
correct |
30 |
Correct |
11 ms |
6432 KB |
correct |
31 |
Correct |
11 ms |
6400 KB |
correct |
32 |
Correct |
10 ms |
6400 KB |
correct |
33 |
Correct |
11 ms |
6528 KB |
correct |
34 |
Correct |
88 ms |
11256 KB |
correct |
35 |
Correct |
650 ms |
37500 KB |
correct |
36 |
Correct |
443 ms |
27896 KB |
correct |
37 |
Correct |
29 ms |
7544 KB |
correct |
38 |
Correct |
660 ms |
38396 KB |
correct |
39 |
Correct |
599 ms |
33656 KB |
correct |
40 |
Correct |
432 ms |
27896 KB |
correct |
41 |
Correct |
32 ms |
8568 KB |
correct |
42 |
Correct |
680 ms |
38496 KB |
correct |
43 |
Correct |
337 ms |
23160 KB |
correct |
44 |
Correct |
278 ms |
19960 KB |
correct |
45 |
Correct |
340 ms |
22008 KB |
correct |
46 |
Correct |
225 ms |
18168 KB |
correct |
47 |
Correct |
98 ms |
11512 KB |
correct |
48 |
Correct |
13 ms |
6528 KB |
correct |
49 |
Correct |
30 ms |
7672 KB |
correct |
50 |
Correct |
102 ms |
11384 KB |
correct |
51 |
Correct |
324 ms |
22136 KB |
correct |
52 |
Correct |
280 ms |
19704 KB |
correct |
53 |
Correct |
230 ms |
18168 KB |
correct |
54 |
Correct |
365 ms |
23288 KB |
correct |
55 |
Correct |
308 ms |
22136 KB |
correct |
56 |
Correct |
332 ms |
22136 KB |
correct |
57 |
Correct |
314 ms |
22148 KB |
correct |
58 |
Correct |
10 ms |
6272 KB |
correct |
59 |
Correct |
9 ms |
6272 KB |
correct |
60 |
Correct |
287 ms |
20512 KB |
correct |
61 |
Correct |
456 ms |
28664 KB |
correct |
62 |
Correct |
481 ms |
29064 KB |
correct |
63 |
Correct |
173 ms |
16780 KB |
correct |
64 |
Correct |
354 ms |
24568 KB |
correct |
65 |
Correct |
348 ms |
23544 KB |
correct |
66 |
Correct |
424 ms |
28024 KB |
correct |
67 |
Correct |
455 ms |
29144 KB |
correct |
68 |
Correct |
415 ms |
27668 KB |
correct |
69 |
Correct |
456 ms |
29048 KB |
correct |
70 |
Correct |
8 ms |
6272 KB |
correct |
71 |
Correct |
367 ms |
25080 KB |
correct |
72 |
Correct |
448 ms |
29024 KB |
correct |
73 |
Correct |
8 ms |
6272 KB |
correct |
74 |
Incorrect |
428 ms |
27768 KB |
WA in grader: NO |
75 |
Halted |
0 ms |
0 KB |
- |