# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
957836 |
2024-04-04T11:35:02 Z |
n3rm1n |
Railway (BOI17_railway) |
C++17 |
|
56 ms |
27404 KB |
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int MAXN = 1e5 + 1, MAXM = 5e4+10, MAXLOG = 21;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int n, m, k;
vector < pair < int, int > > g[MAXN];
void read_graph()
{
cin >> n >> m >> k;
int from, to;
for (int i = 1; i < n; ++ i)
{
cin >> from >> to;
g[from].push_back({to, i});
g[to].push_back({from, i});
}
}
int p[MAXN], lvl[MAXN], tin[MAXN], tout[MAXN];
int tt = 0;
void dfs(int beg, int par, int depth)
{
tt ++;
p[beg] = par;
lvl[beg] = depth;
tin[beg] = tt;
int nb;
for (int i = 0; i < g[beg].size(); ++ i)
{
nb = g[beg][i].first;
if(lvl[nb] == 0)
{
dfs(nb, beg, depth+1);
tout[beg] = max(tout[beg], tout[nb] + 1);
}
}
}
int jump[MAXN][MAXLOG];
void fill_jump()
{
for (int i = 1; i <= n; ++ i)
jump[i][0] = p[i];
for (int j = 1; j < MAXLOG; ++ j)
for (int i = 1; i <= n; ++ i)
jump[i][j] = jump[jump[i][j-1]][j-1];
}
int move_up(int v, int dist)
{
for (int i = 0; (1 << i) <= dist; ++ i)
{
if((1 << i) & dist)
v = jump[v][i];
}
return v;
}
int lca(int u, int v)
{
if(lvl[u] > lvl[v])swap(u, v);
int diff = lvl[v] - lvl[u];
v = move_up(v, diff);
if(u == v)return u;
for (int i = MAXLOG - 1; i >= 0; -- i)
{
if(jump[u][i] != jump[v][i])
{
u = jump[u][i];
v = jump[v][i];
}
}
return p[u];
}
int s;
bool cmp(int x, int y)
{
return (tout[x] < tout[y]);
}
int cnt[MAXN];
int ans[MAXN];
int dfs2(int beg, int par)
{
int take_up = cnt[beg];
int nb, id;
int from_down;
for (int i = 0; i < g[beg].size(); ++ i)
{
nb = g[beg][i].first;
id = g[beg][i].second;
if(nb != par)
{
from_down = dfs2(nb, beg);
take_up += from_down;
ans[id] += from_down;
}
}
return take_up;
}
void read_queries()
{
int xx, lcaa;
vector < int > que;
for (int i = 1; i <= m; ++ i)
{
cin >> s >> xx;
que.clear();
que.push_back(xx);
for (int j = 2; j <= s; ++ j)
{
cin >> xx;
que.push_back(xx);
}
sort(que.begin(), que.end(), cmp);
lcaa = que[0];
for (int j = 1; j < que.size(); ++ j)
{
xx = que[j];
cnt[lcaa] ++;
cnt[xx] ++;
lcaa = lca(lcaa, xx);
cnt[lcaa] -= 2;
}
}
}
int main()
{
speed();
read_graph();
dfs(1, 0, 1);
fill_jump();
read_queries();
dfs2(1, 0);
vector < int > anss;
for (int i = 1; i <= n; ++ i)
{
// cout << ans[i] << " ";
if(ans[i] >= k)anss.push_back(i);
}
cout << anss.size() << endl;
for (int i = 0; i < anss.size(); ++ i)
cout << anss[i] << " ";
cout << endl;
return 0;
}
Compilation message
railway.cpp: In function 'void dfs(int, int, int)':
railway.cpp:34:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int i = 0; i < g[beg].size(); ++ i)
| ~~^~~~~~~~~~~~~~~
railway.cpp: In function 'int dfs2(int, int)':
railway.cpp:90:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for (int i = 0; i < g[beg].size(); ++ i)
| ~~^~~~~~~~~~~~~~~
railway.cpp: In function 'void read_queries()':
railway.cpp:122:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for (int j = 1; j < que.size(); ++ j)
| ~~^~~~~~~~~~~~
railway.cpp: In function 'int main()':
railway.cpp:152:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
152 | for (int i = 0; i < anss.size(); ++ i)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
6 ms |
5464 KB |
Output is correct |
3 |
Incorrect |
4 ms |
5468 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
6 ms |
5464 KB |
Output is correct |
3 |
Incorrect |
4 ms |
5468 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
26824 KB |
Output is correct |
2 |
Correct |
1 ms |
4952 KB |
Output is correct |
3 |
Correct |
55 ms |
26672 KB |
Output is correct |
4 |
Correct |
46 ms |
25920 KB |
Output is correct |
5 |
Correct |
50 ms |
26824 KB |
Output is correct |
6 |
Correct |
54 ms |
27404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
22604 KB |
Output is correct |
2 |
Correct |
45 ms |
19120 KB |
Output is correct |
3 |
Correct |
56 ms |
18672 KB |
Output is correct |
4 |
Correct |
45 ms |
18848 KB |
Output is correct |
5 |
Correct |
45 ms |
18768 KB |
Output is correct |
6 |
Correct |
46 ms |
22648 KB |
Output is correct |
7 |
Correct |
44 ms |
22608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
22604 KB |
Output is correct |
2 |
Correct |
45 ms |
19120 KB |
Output is correct |
3 |
Correct |
56 ms |
18672 KB |
Output is correct |
4 |
Correct |
45 ms |
18848 KB |
Output is correct |
5 |
Correct |
45 ms |
18768 KB |
Output is correct |
6 |
Correct |
46 ms |
22648 KB |
Output is correct |
7 |
Correct |
44 ms |
22608 KB |
Output is correct |
8 |
Incorrect |
49 ms |
23044 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
6 ms |
5464 KB |
Output is correct |
3 |
Incorrect |
4 ms |
5468 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |