Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 – Elimination Round 3), problem: (F) Santa Clauses and a Soccer Championship Solution in C/C++

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 – Elimination Round 3), problem: (F) Santa Clauses and a Soccer Championship Solution in C/C++

#include <bits/stdc++.h>

using namespace std;

vector<int> g[200005], lst[2];

int n, k, c, s[200005], p;
bool f[200005];

void dfs(int x, int fa = -1) {
	if(f[x]) 
		lst[++c > k].push_back(x);
	for(auto y: g[x]) {
		if(y == fa) continue;
		dfs(y, x);
		s[x] += s[y];
		if(s[x] >= k and !p) p = x;
	}
}

int main() {
	scanf("%d%d", &n, &k);
	for(int i = 1;i < n;i++) {
		int a, b; scanf("%d%d", &a, &b);
		g[a].push_back(b), g[b].push_back(a);
	}
	for(int i = 0;i < 2 * k;i++) {
		int x; scanf("%d", &x);
		f[x] = true; s[x] = 1;
	}
	dfs(1);
	printf("1\n%d\n", p);
	for(int i = 0;i < k;i++)
			printf("%d %d %d\n", lst[0][i], lst[1][i], p);
}

📦 Looking for Codeforces Round 389 Div Based Technocup? Check the best deals on Amazon.

🛒 Shop Codeforces Round 389 Div Based Technocup on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 – Elimination Round 3), problem: (F) Santa Clauses and a Soccer Championship Solution in C/C++

#include <bits/stdc++.h>

using namespace std;

vector<int> g[200005], lst[2];

int n, k, c, s[200005], p;
bool f[200005];

void dfs(int x, int fa = -1) {
	if(f[x]) 
		lst[++c > k].push_back(x);
	for(auto y: g[x]) {
		if(y == fa) continue;
		dfs(y, x);
		s[x] += s[y];
		if(s[x] >= k and !p) p = x;
	}
}

int main() {
	scanf("%d%d", &n, &k);
	for(int i = 1;i < n;i++) {
		int a, b; scanf("%d%d", &a, &b);
		g[a].push_back(b), g[b].push_back(a);
	}
	for(int i = 0;i < 2 * k;i++) {
		int x; scanf("%d", &x);
		f[x] = true; s[x] = 1;
	}
	dfs(1);
	printf("1\n%d\n", p);
	for(int i = 0;i < k;i++)
			printf("%d %d %d\n", lst[0][i], lst[1][i], p);
}

📦 Looking for Codeforces Round 389 Div Based Technocup? Check the best deals on Amazon.

🛒 Shop Codeforces Round 389 Div Based Technocup on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Everything You Need to Know About Viral Kitchen Gadgets Right Now

TOP RATED PICK 2026 Top Recommended Product ★★★★★ (4.8 out of 5) 🔹 High build quality 🔹 Excellent value for...

What’s new in Gutenberg 24.0? (16 September)

The WordPress open-source community has officially unveiled Gutenberg 24.0, marking another milestone in the project’s biweekly development cycle. This release brings a focused batch...

The Real Truth About Viral Kitchen Gadgets Before You Buy

TOP RATED PICK 2026 Top Recommended Product ★★★★★ (4.8 out of 5) 🔹 High build quality 🔹 Excellent value for...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!