-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinding cetnroid of a tree.cpp
216 lines (146 loc) · 4.68 KB
/
Finding cetnroid of a tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include<bits/stdc++.h> // "Hello there"
using namespace std;
// ->uncomment for ordered set
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// // typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // for pair
// typedef tree<int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// ->uncomment for unordered map of pairs.
// #include<functional>
// template <typename T>
// inline void hash_combine(std::size_t &seed, const T &val) {seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);}
// template <typename T> inline void hash_val(std::size_t &seed, const T &val) {hash_combine(seed, val);}
// template <typename T, typename... Types>
// inline void hash_val(std::size_t &seed, const T &val, const Types &... args) {hash_combine(seed, val); hash_val(seed, args...);}
// template <typename... Types>
// inline std::size_t hash_val(const Types &... args) { std::size_t seed = 0; hash_val(seed, args...);return seed;}
// struct pair_hash
// {
// template <class T1, class T2>
// std::size_t operator()(const std::pair<T1, T2> &p) const
// {
// return hash_val(p.first, p.second);
// }
// };
#define pb push_back
#define ll long long int
#define SORTA(v) sort(v.begin(),v.end())
#define SORTD(v) sort(v.begin(),v.end(),greater<>())
#define endl '\n'
#define FOR(i,n) for(ll i = 0;i<n;i++)
#define FOR1(i,n) for(ll i=1;i<=n;i++)
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define REVERSE(s) reverse(s.begin(),s.end())
#define GCD(a,b) __gcd(a,b)
#define VFIND(v1,val) find(v1.begin() , v1.end() , val )
#define VFILL(v1 , val) std::fill(v1.begin(), v1.end(), val);
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
const int MOD = 1e9 + 7;
long long int INF = LLONG_MAX;
void DPM(map<ll,ll>m1)
{
for(auto it = m1.begin();it!=m1.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
cout<<endl;
}
void DPV(vector<ll>v)
{
FOR(i,v.size())cout<<v[i]<<" ";cout<<endl;
}
void DPS(set<ll>s1)
{
for(auto it =s1.begin();it!=s1.end();it++)cout<<*it<<" ";cout<<endl;
}
ll dx[4] = { 0,0,1,-1 };
ll dy[4] = { 1,-1,0,0 };
string pos = "RLDU";
// check the limits dummy
const ll NN = 1* (1e5) + 100;
vector< vector<ll> > adj(NN);
vector<ll>child(NN) , vis(NN);
ll n , centroid_1 , centroid_2;
void Dfs(ll v )
{
if( vis[v] )return;
vis[v] = child[v] = 1;
bool is_centroid = true;
for(auto u : adj[v] )
{
if(!vis[u] )
{
Dfs(u);
child[v] += child[u];
if( child[u] > n/2 )is_centroid = false;
}
}
if(n - child[v] > n/2 )
{
is_centroid = false;
}
if(is_centroid)
{
if(centroid_1 == -1)centroid_1 = v;
else centroid_2 = v;
}
}
int main()
{
FAST;
ll i ,j , TC = 1;
cin>>TC;
FOR1(T,TC)
{
cin>>n;
FOR1(i,n-1)
{
ll a,b; cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
centroid_1 = centroid_2 = -1;
Dfs(1);
if(centroid_2 == -1)
{
cout<<centroid_1<<" "<<adj[ centroid_1][0]<<endl;
cout<<centroid_1<<" "<<adj[ centroid_1][0]<<endl;
}
else
{
if( adj[centroid_1].size() < adj[centroid_2].size() )
{
swap(centroid_1 , centroid_2);
}
int x;
for(auto u : adj[centroid_1] )
{
if(u != centroid_2 )
{
x = u;
break;
}
}
cout<<centroid_1<<" "<<x<<endl;
cout<<centroid_2<<" "<<x<<endl;
}
FOR1(i,n)adj[i].clear() , child[i] = 0 , vis[i] = 0;
}
}
/* Note that finding centroids != Finding roots with min heigt..
For min heights see leetcode ques.
5
1 3
2 3 -> work this example out.
4 1
5 1
*/
// vector<vector<int>> vec( n , vector<int> (m, 0)); 2d vector
// to_string(a)
// s1.substr(1,3) 3 char of s1 starting from 1 s1.sub(pos) starting from pos
// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z) is(65-90) and 32 for space
// reverse(v1.begin() + a , v1.begin() + b) reverse starting from a to b-1
// A lambda function is written like [](argument1,argument2,.....){//code}
// to create unordered map for pairs unordered_map<pair<ll, ll>, ll, pair_hash> slopeCount;
// unordered_set<pair<ll, ll>, pair_hash> seen;