Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <set>
- #include <vector>
- #include <cstring>
- #define task "egalitarianism3"
- using namespace std;
- using ll = long long;
- using ld = long double;
- const int N = 50 + 2;
- vector<pair<int, int>> adj[N];
- int n, chosen, val, par[N], dp[N];
- const int lim = 49000;
- int ans = 0, cnt[N][lim + 2], tmp[lim + 2];
- void Read(){
- cin >> n;
- for(int i = 1; i < n; ++i){
- int u, v, w;
- cin >> u >> v >> w;
- adj[u].push_back({v, w});
- adj[v].push_back({u, w});
- }
- }
- void dfs(int v){
- //cout << v << ": ";
- for(auto i : adj[v])
- if(i.first != par[v]){
- par[i.first] = v;
- dp[i.first] = i.second;
- dfs(i.first);
- for(int j = i.second; j <= lim; ++j)
- if(cnt[i.first][j - i.second])
- ++cnt[v][j];
- }
- memset(tmp, 0, sizeof tmp);
- int sum = 0, piv = v;
- while(piv != 0){
- ++tmp[sum];
- sum += dp[piv];
- piv = par[piv];
- }
- for(int j = 0; j <= lim; ++j)
- if(ans < cnt[v][j] + tmp[j]){
- ans = cnt[v][j] + tmp[j];
- chosen = v;
- val = j;
- }
- cnt[v][0] = 1;
- //cout << ans << ' ' << chosen << " " << val << "\n";
- }
- set<int> res;
- bool dfs_(int v, int sum){
- if(sum == 0){
- res.insert(v);
- return true;
- }
- if(sum < 0)
- return false;
- for(auto i : adj[v])
- if(i.first != par[v])
- if(dfs_(i.first, sum - i.second))
- return true;
- return false;
- }
- void Solve(){
- dfs(1);
- if(ans > 1 || n == 1){
- cout << ans << "\n";
- //cout << chosen << " " << val << "\n";
- int sum = 0, piv = chosen;
- while(piv != 0 && sum <= val){
- if(sum == val)
- res.insert(piv);
- sum += dp[piv];
- piv = par[piv];
- }
- for(auto i : adj[chosen])
- if(i.first != par[chosen])
- dfs_(i.first, val - i.second);
- for(auto i : res)
- cout << i << " ";
- }
- else{
- cout << "2\n1 2";
- }
- }
- int32_t main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0); if(fopen(task".INP", "r"))
- freopen(task".INP", "r", stdin),
- freopen(task".OUT", "w", stdout);
- Read();
- Solve();
- }
- /*
- 8
- 1 2 1
- 2 3 2
- 3 4 2
- 4 5 3
- 5 6 3
- 6 7 2
- 7 8 1
- */
Advertisement
Add Comment
Please, Sign In to add comment