Submission #969175

# Submission time Handle Problem Language Result Execution time Memory
969175 2024-04-24T16:01:50 Z pan Pastiri (COI20_pastiri) C++17
92 / 100
1000 ms 107108 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef __int128  sll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> piii;
ll const INF = 1e13;
ll n, k, a, b;
vector<ll> sheep;
vector<ll> adj[500005];
vector<ll> ans;
ll depth[500005], dist[500005], visited[500005], dp[500005], save[500005], done[500005];

bool compare(ll x, ll y)
{
	return depth[x] > depth[y];
}
void dfs(ll from, ll to)
{
	if (dist[to] + depth[to] > dp[to]) dp[to] = dist[to] + depth[to], save[to] = to;
	for (ll u: adj[to])
	{
		if (u==from) continue;
		depth[u] = depth[to] + 1;
		dp[u] = dp[to];
		save[u] = save[to];
		dfs(to, u);
	}
}


void mark( ll to)
{
	done[to] = true;
	for (ll u: adj[to])
	{
		if (done[u] || dist[u]!=dist[to]-1) continue;
		mark(u);
	}
}
int main()
{
	input2(n, k);
	for (ll i=0; i<n-1; ++i) 
	{
		input2(a, b);
		adj[a].pb(b);
		adj[b].pb(a);
	}
	sheep.resize(k);
	fill(dist+1, dist+n+1, INF);
	fill(visited+1, visited+n+1, 0);
	queue<ll> q;
	for (ll i=0; i<k; ++i) {input(sheep[i]); q.push(sheep[i]); dist[sheep[i]] = 0;}
	while (q.size())
	{
		ll u = q.front();
		q.pop();
		if (visited[u]) continue;
		visited[u] = true;
		for (ll x: adj[u]) if (dist[x] > dist[u] + 1) {dist[x] = dist[u] + 1; q.push(x);}
	}
	fill(dp+1, dp+n+1, -INF);
	depth[1] = 0;
	dfs(-1, 1);
	fill(done+1, done+n+1, 0);
	sort(sheep.begin(), sheep.end(), compare);
	for (ll u: sheep)
	{
		if (done[u]) continue;
		show(u);
		mark(save[u]);
		ans.pb(save[u]);
	}
	print(ll(ans.size()), '\n');
	for (ll u: ans) print(u, ' ');
	return 0;
}

Compilation message

pastiri.cpp: In function 'int main()':
pastiri.cpp:13:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define input2(x, y) scanf("%lld%lld", &x, &y);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~
pastiri.cpp:70:2: note: in expansion of macro 'input2'
   70 |  input2(n, k);
      |  ^~~~~~
pastiri.cpp:13:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define input2(x, y) scanf("%lld%lld", &x, &y);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~
pastiri.cpp:73:3: note: in expansion of macro 'input2'
   73 |   input2(a, b);
      |   ^~~~~~
pastiri.cpp:12:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | #define input(x) scanf("%lld", &x);
      |                  ~~~~~^~~~~~~~~~~~
pastiri.cpp:81:26: note: in expansion of macro 'input'
   81 |  for (ll i=0; i<k; ++i) {input(sheep[i]); q.push(sheep[i]); dist[sheep[i]] = 0;}
      |                          ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 143 ms 96844 KB Output is correct
2 Correct 178 ms 96848 KB Output is correct
3 Correct 174 ms 96852 KB Output is correct
4 Execution timed out 1028 ms 107108 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 23620 KB Output is correct
2 Correct 6 ms 23388 KB Output is correct
3 Correct 325 ms 61308 KB Output is correct
4 Correct 360 ms 66376 KB Output is correct
5 Correct 427 ms 57980 KB Output is correct
6 Correct 569 ms 61468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 23128 KB Output is correct
2 Correct 5 ms 23236 KB Output is correct
3 Correct 7 ms 23132 KB Output is correct
4 Correct 7 ms 23132 KB Output is correct
5 Correct 7 ms 23132 KB Output is correct
6 Correct 7 ms 23132 KB Output is correct
7 Correct 5 ms 23132 KB Output is correct
8 Correct 5 ms 23132 KB Output is correct
9 Correct 5 ms 23132 KB Output is correct
10 Correct 5 ms 23132 KB Output is correct
11 Correct 6 ms 23132 KB Output is correct
12 Correct 5 ms 23252 KB Output is correct
13 Correct 7 ms 23132 KB Output is correct
14 Correct 6 ms 23132 KB Output is correct
15 Correct 7 ms 23132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 523 ms 61728 KB Output is correct
2 Correct 476 ms 61908 KB Output is correct
3 Correct 902 ms 67704 KB Output is correct
4 Correct 431 ms 57828 KB Output is correct
5 Correct 816 ms 61400 KB Output is correct
6 Correct 549 ms 72388 KB Output is correct
7 Correct 901 ms 70500 KB Output is correct
8 Correct 898 ms 69256 KB Output is correct
9 Correct 544 ms 57936 KB Output is correct
10 Correct 462 ms 58044 KB Output is correct
11 Correct 386 ms 63812 KB Output is correct
12 Correct 319 ms 67760 KB Output is correct
13 Correct 327 ms 71012 KB Output is correct
14 Correct 307 ms 64636 KB Output is correct
15 Correct 39 ms 33620 KB Output is correct
16 Correct 496 ms 56908 KB Output is correct
17 Correct 818 ms 62256 KB Output is correct
18 Correct 489 ms 53728 KB Output is correct
19 Correct 632 ms 68344 KB Output is correct
20 Correct 574 ms 72836 KB Output is correct
21 Correct 416 ms 60572 KB Output is correct
22 Correct 408 ms 61096 KB Output is correct