// हर हर महादेव
using namespace std;
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
#ifdef shivang_ka_laptop
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__);
#define booga cerr << "booga" << endl;
#else
#define debug(...) 42;
#define booga 9;
#endif
template <typename T> std::ostream& operator<<(std::ostream& stream, const vector<T>& vec){ for(size_t i = 0; i < vec.size(); i++){stream << vec[i];if(i != vec.size() - 1)stream << ' ';}; return stream; }
template <typename T> std::istream& operator>>(std::istream& stream, vector<T>& vec) { for(T &x : vec)stream >> x;return stream; }
template <typename T,typename U> std::ostream& operator<<(std::ostream& stream, const pair<T,U>& pr){ stream << pr.first << ' ' << pr.second; return stream; }
template <typename T,typename U> std::istream& operator>>(std::istream& stream, pair<T,U>& pr){ stream >> pr.first >> pr.second; return stream; }
template <typename T, typename U> void operator+=(vector<T>& vec, const U value) { for(T &x : vec)x += value; }
template <typename T, typename U> void operator-=(vector<T>& vec, const U value) { for(T &x : vec)x -= value; }
template <typename T, typename U> void operator*=(vector<T>& vec, const U value) { for(T &x : vec)x *= value; }
template <typename T, typename U> void operator/=(vector<T>& vec, const U value) { for(T &x : vec)x /= value; }
template <typename T> void operator++(vector<T>& vec) { vec += 1; }
template <typename T> void operator++(vector<T>& vec,int) { vec += 1; }
template <typename T> void operator--(vector<T>& vec) { vec -= 1; }
template <typename T> void operator--(vector<T>& vec,int) { vec -= 1; }
template <typename T,typename U, typename V> void operator+=(pair<T,U>& vec, const V value) { vec.first += value;vec.second += value; }
template <typename T,typename U, typename V> void operator-=(pair<T,U>& vec, const V value) { vec.first -= value;vec.second -= value; }
template <typename T,typename U, typename V> void operator*=(pair<T,U>& vec, const V value) { vec.first *= value;vec.second *= value; }
template <typename T,typename U, typename V> void operator/=(pair<T,U>& vec, const V value) { vec.first /= value;vec.second /= value; }
template <typename T,typename U> void operator++(pair<T,U>& vec) { vec += 1; }
template <typename T,typename U> void operator++(pair<T,U>& vec,int) { vec += 1; }
template <typename T,typename U> void operator--(pair<T,U>& vec) { vec -= 1; }
template <typename T,typename U> void operator--(pair<T,U>& vec,int) { vec -= 1; }
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(char c) {string s;s += c;return s; }
string to_string(const char* s) {return to_string((string) s); }
string to_string(bool b) {return (b ? "1" : "0"); }
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
void debug_out() { cerr << endl; } template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
void bharo(int N_N) { return; }template <typename Head, typename... Tail>void bharo(int N_N, Head &H, Tail & ... T) {H.resize(N_N);bharo(N_N,T...);}
void safai() { return; }template <typename Head, typename... Tail>void safai(Head &H, Tail & ... T) {H.clear();safai(T...);}
vector<int> pre;
vector<int> dist;
void dijkstra(int start,vector<vector<int>> &adj){
dist.clear();dist.resize(adj.size(),1e9 + 50);
pre.clear();pre.resize(adj.size(),-1);
queue<pair<int,int>> que;
que.push({dist[start] = 0, start});
while (!que.empty()) {
auto top = que.front();
que.pop();
if (top.first != dist[top.second]) continue;
for(auto node : adj[top.second])
if(dist[node] > top.first + 1)
que.push({dist[node] = top.first + 1, node}), pre[node] = top.second;
}
}
pair<vector<int>,vector<int>> get(int root,vector<vector<int>> &graph){
int n = graph.size();
vector<int> sub(n,1);
vector<int> par(n,-1);
function<void(int,int)> dfs = [&](int i,int parent){
par[i] = parent;
for(int node : graph[i]){
if(node != parent){
dfs(node,i);
sub[i] += sub[node];
}
}
}; dfs(root,-1);
return {sub,par};
}
void testcase(){
int n;
cin >> n;
vector<vector<int>> graph(n);
for(int i = 0; i < n-1; i++){
int u,v;
cin >> u >> v;
u--;v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
dijkstra(0,graph);
int r1 = max_element(dist.begin(),dist.end()) - dist.begin();
dijkstra(r1,graph);
int r2 = max_element(dist.begin(),dist.end()) - dist.begin();
int ans = 1 + dist[r2];
auto [s1,p1] = get(r2,graph);
auto [s2,p2] = get(r1,graph);
vector<vector<int>> s(2);
vector<vector<int>> p(2);
s[0] = s1;p[0] = p1;
s[1] = s2;p[1] = p2;
set<int> all[2];
vector<int> have(2,1);
vector<int> end(2);
end[0] = r1;
end[1] = r2;
all[0] = {r1};
all[1] = {r2};
//debug(end)
for(int i = 1; i <= n; i++){
if(i & 1){
cout << 1 << '\n';
continue;
}
int need = i / 2;
bool ok = true;
for(int j = 0; j < 2 && ok; j++){
while(have[j] < need){
int nxt = p[j][end[j]];
if(nxt == -1 || all[j ^ 1].find(nxt) != all[j ^ 1].end()){
ok = false;
break;
}
all[j].insert(nxt);
ans--;
for(int node : graph[nxt]){
if(node != end[j] && node != p[j][nxt]){
have[j] += s[j][node];
}
}
have[j]++;
end[j] = nxt;
//debug(end,have,nxt)
}
}
if(!ok || ans < 1)ans = 1;
cout << ans << '\n';
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
//cin >> tt;
while(tt--){
testcase();
}
return (0-0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |