Submission #73718244
Source Code Expand
#include<bits/stdc++.h>
//入力系
#define cinll(...) ll __VA_ARGS__; input(__VA_ARGS__);
#define cinint(...) int __VA_ARGS__; input(__VA_ARGS__);
#define cinstr(...) string __VA_ARGS__; input(__VA_ARGS__);
#define cinchar(...) char __VA_ARGS__; input(__VA_ARGS__);
#define cindouble(...) double __VA_ARGS__; input(__VA_ARGS__);
#define cinvll(a,n) vll a(n); rep(i,n) cin>>a[i];
#define cinvvll(a,n,m) vvll a(n,vll(m)); rep(i,n) rep(j,m) cin>>a[i][j];
#define cinvs(a,n) vs a(n); rep(i,n) cin>>a[i];
#define cinvpll(a,n) vpll a(n); rep(i,n) cin>>a[i].fst>>a[i].snd;
//繰り返し系
#define rep1(n) for(ll i=0;i<n;i++)
#define rep2(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,n) for(ll i=a;i<n;i++)
#define rep4(i,a,n,b) for(ll i=a;i<n;i+=b)
#define overload4(a,b,c,d,e,...) e
#define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define mrep1(n) for(ll i=n;i>=0;i--)
#define mrep2(i,n) for(ll i=n;i>=0;i--)
#define mrep3(i,n,a) for(ll i=n;i>=a;i--)
#define mrep4(i,n,a,b) for(ll i=n;i>=a;i-=b)
#define mrep(...) overload4(__VA_ARGS__,mrep4,mrep3,mrep2,mrep1)(__VA_ARGS__)
//iterator系
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
//vector系
#define pb push_back
//書くのが長いやつ
#define fst first
#define snd second
#define cvvll1(name,a,b) vvll name(a, vll(b))
#define cvvll2(name,a,b,c) vvll name(a, vll(b,c))
#define cvvlloverload2(name,a,b,c,d,...) d
#define make_vvll(...) cvvlloverload2(__VA_ARGS__,cvvll2,cvvll1)(__VA_ARGS__)
using namespace std;
//型系
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
using pll = pair<long long,long long>;
using pi = pair<int,int>;
using pd = pair<double,double>;
using sll = set<long long>;
using vsll = vector<sll>;
using vpll = vector<pll>;
using vpi = vector<pi>;
using vpd = vector<pd>;
using vvpll = vector<vpll>;
#define vmll vector<mll>
#define vvmll vector<vector<mll>>
const ll mod = 998244353LL;
// const ll mod = 1000000007LL;
const ll inf = 1300100100100100100LL;
const double PI=3.1415926535897932384626433832795028841971;
//表示
#define overload1(a,b,NAME,...) NAME
#define coutYesReturn() do {coutYes(); return 0; } while(0)
#define coutYesReturnIf(a) do { if(a){ coutYesReturn(); }} while(0)
#define coutNoReturn() do {coutNo(); return 0;} while(0)
#define coutNoReturnIf(a) do {if(a){ coutNoReturn(); }} while(0)
#define coutReturnIf(a,s) do{if(a){cout<<s<<endl; return 0;}}while(0)
template<typename... T>
void coutll(T... a){ ((cout << a <<" "),...) << endl; }
void coutvll(vll &a){ rep(i,a.size()) cout<<a[i]<<" "; cout<<endl; }
void coutvll(string name, vll &a){ cout<<name<<":"; coutvll(a); }
void coutvlln(vll &a){ rep(i,a.size()) cout<<a[i]<<endl; }
void coutYes(){ cout<<"Yes"<<endl; }
void coutNo(){ cout<<"No"<<endl; }
void coutYesNo(bool a){ cout<<(a?"Yes":"No")<<endl; }
void coutIf(bool a, string s, string t){ cout<<(a?s:t)<<endl; }
//入力
template<class... T>
void input(T&... a){
(cin >> ... >> a);
}
//複数ソート
template<class... T>
void sorts(vector<T>&... a){
(sort(all(a)),...);
}
//便利関数
template<typename T> bool chmax(T &a, T b){ if(a < b) {a = b; return true;} return false; }
template<typename T> bool chmin(T &a, T b){ if(a > b) {a = b; return true;} return false; }
//配列表示用
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
rep(i,vec.size()) os << vec[i] << (i==(ll)vec.size()-1?"":" ");
return os;
}
struct UnionFind {
public:
vll par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
vll kosu;
ll Count; //全体として何個の集合があるか
UnionFind(ll n) : par(n),kosu(n),Count(n) { //最初は全てが根であるとして初期化
rep(i,n){
par[i] = i;
kosu[i] = 1;
}
}
ll root(ll x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(ll x, ll y) { // xとyの木を併合
ll rx = root(x); //xの根をrx
ll ry = root(y); //yの根をry
if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] = ry; //xとyの根
kosu[ry] += kosu[rx];
Count--;
}
bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
ll rx = root(x);
ll ry = root(y);
return rx == ry;
}
ll size(ll x){
return kosu[root(x)];
}
ll size(){
return Count;
}
};
//木に対して、頂点から最も遠い点(複数ある場合はどれか一つ)の{距離、番号}を返す
//treeLongestDistanceFromRoot(nexts, s)のように使う
pll treeLongestDistanceFromRoot(vvll &nexts, ll now, ll prev, vb &visit){
ll ret = 0;
ll reti = now;
visit[now] = true;
for(ll next: nexts[now]) if(next != prev){
pll nl = treeLongestDistanceFromRoot(nexts, next, now, visit);
if(nl.first+1 >= ret){
ret = nl.first+1;
reti = nl.second;
}
}
return {ret, reti};
}
struct S{
ll c, u, v;
};
//直径をもとめる
S treeDiameter(vvll &nexts, vb &visit, ll s){
pll p1 = treeLongestDistanceFromRoot(nexts, s,-1, visit);
pll p2 = treeLongestDistanceFromRoot(nexts, p1.snd,-1, visit);
return S{p2.fst, p1.snd, p2.snd};
}
int solve(){
cinll(n);
vvll nexts(n);
rep(i, n-1){
cinll(u, v);
u--; v--;
nexts[u].pb(v);
nexts[v].pb(u);
}
vvll tnexts(n);
rep(i, n){
for(ll j: nexts[i]){
if(nexts[i].size() <= 2 || nexts[j].size() <= 2) continue;
tnexts[i].pb(j);
}
}
// rep(i, n){
// cout << i+1 << " : ";
// for(ll j: tnexts[i]) cout << j << " ";
// cout << endl;
// }
vb visit(n, false);
ll ans = 0;
rep(i, n) if(!visit[i]) {
S t = treeDiameter(tnexts, visit, i);
//cout << "i: " << i << " " << t << endl;
chmax(ans, t.c);
}
cout << ans +1 << endl;
return 0;
}
int main(){
cinll(t);
while(t--) solve();
return 0;
}
Submission Info
| Submission Time |
|
| Task |
F - Centipede Graph |
| User |
hoppii |
| Language |
C++23 (GCC 15.2.0) |
| Score |
0 |
| Code Size |
6657 Byte |
| Status |
WA |
| Exec Time |
125 ms |
| Memory |
27344 KiB |
Compile Error
./Main.cpp: In function 'void coutvll(vll&)':
./Main.cpp:14:31: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | #define rep2(i,n) for(ll i=0;i<n;i++)
| ^
./Main.cpp:17:34: note: in expansion of macro 'rep2'
17 | #define overload4(a,b,c,d,e,...) e
| ^
./Main.cpp:18:18: note: in expansion of macro 'overload4'
18 | #define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
| ^~~~~~~~~
./Main.cpp:76:23: note: in expansion of macro 'rep'
76 | void coutvll(vll &a){ rep(i,a.size()) cout<<a[i]<<" "; cout<<endl; }
| ^~~
./Main.cpp: In function 'void coutvlln(vll&)':
./Main.cpp:14:31: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | #define rep2(i,n) for(ll i=0;i<n;i++)
| ^
./Main.cpp:17:34: note: in expansion of macro 'rep2'
17 | #define overload4(a,b,c,d,e,...) e
| ^
./Main.cpp:18:18: note: in expansion of macro 'overload4'
18 | #define rep(...) overload4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
| ^~~~~~~~~
./Main.cpp:78:24: note: in expansion of macro 'rep'
78 | void coutvlln(vll &a){ rep(i,a.size()) cout<<a[i]<<endl; }
| ^~~
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
0 / 500 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
00_sample_00.txt |
| All |
00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 03_path_00.txt, 03_path_01.txt, 03_path_02.txt, 03_path_03.txt, 03_path_04.txt, 03_path_05.txt, 03_path_06.txt, 03_path_07.txt, 03_path_08.txt, 03_path_09.txt, 04_star_00.txt, 04_star_01.txt, 04_star_02.txt, 05_uni_00.txt, 05_uni_01.txt, 05_uni_02.txt, 05_uni_03.txt, 05_uni_04.txt, 05_uni_05.txt, 06_bi-ternary_00.txt, 06_bi-ternary_01.txt, 06_bi-ternary_02.txt, 06_bi-ternary_03.txt, 06_bi-ternary_04.txt, 06_bi-ternary_05.txt, 06_bi-ternary_06.txt, 06_bi-ternary_07.txt, 06_bi-ternary_08.txt, 06_bi-ternary_09.txt, 06_bi-ternary_10.txt, 06_bi-ternary_11.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 00_sample_00.txt |
AC |
1 ms |
3592 KiB |
| 01_small_00.txt |
WA |
56 ms |
3412 KiB |
| 01_small_01.txt |
WA |
56 ms |
3632 KiB |
| 01_small_02.txt |
WA |
56 ms |
3412 KiB |
| 01_small_03.txt |
WA |
57 ms |
3460 KiB |
| 01_small_04.txt |
WA |
56 ms |
3556 KiB |
| 01_small_05.txt |
WA |
56 ms |
3592 KiB |
| 01_small_06.txt |
WA |
35 ms |
3412 KiB |
| 02_random_00.txt |
WA |
68 ms |
3588 KiB |
| 02_random_01.txt |
WA |
68 ms |
3760 KiB |
| 02_random_02.txt |
WA |
68 ms |
3720 KiB |
| 02_random_03.txt |
WA |
106 ms |
19116 KiB |
| 02_random_04.txt |
WA |
92 ms |
11828 KiB |
| 02_random_05.txt |
WA |
99 ms |
15912 KiB |
| 02_random_06.txt |
WA |
98 ms |
13892 KiB |
| 02_random_07.txt |
WA |
92 ms |
11388 KiB |
| 03_path_00.txt |
WA |
68 ms |
3748 KiB |
| 03_path_01.txt |
WA |
68 ms |
3684 KiB |
| 03_path_02.txt |
WA |
77 ms |
4572 KiB |
| 03_path_03.txt |
WA |
77 ms |
4780 KiB |
| 03_path_04.txt |
WA |
92 ms |
11684 KiB |
| 03_path_05.txt |
WA |
92 ms |
11872 KiB |
| 03_path_06.txt |
AC |
115 ms |
27332 KiB |
| 03_path_07.txt |
WA |
117 ms |
27332 KiB |
| 03_path_08.txt |
AC |
122 ms |
27344 KiB |
| 03_path_09.txt |
WA |
119 ms |
27328 KiB |
| 04_star_00.txt |
AC |
88 ms |
20484 KiB |
| 04_star_01.txt |
WA |
71 ms |
4576 KiB |
| 04_star_02.txt |
WA |
89 ms |
12576 KiB |
| 05_uni_00.txt |
WA |
69 ms |
3544 KiB |
| 05_uni_01.txt |
WA |
77 ms |
4260 KiB |
| 05_uni_02.txt |
WA |
94 ms |
11340 KiB |
| 05_uni_03.txt |
AC |
115 ms |
22104 KiB |
| 05_uni_04.txt |
WA |
116 ms |
22136 KiB |
| 05_uni_05.txt |
WA |
118 ms |
22204 KiB |
| 06_bi-ternary_00.txt |
WA |
112 ms |
22724 KiB |
| 06_bi-ternary_01.txt |
WA |
94 ms |
11956 KiB |
| 06_bi-ternary_02.txt |
WA |
71 ms |
3540 KiB |
| 06_bi-ternary_03.txt |
WA |
110 ms |
22580 KiB |
| 06_bi-ternary_04.txt |
WA |
100 ms |
11336 KiB |
| 06_bi-ternary_05.txt |
WA |
71 ms |
3676 KiB |
| 06_bi-ternary_06.txt |
WA |
111 ms |
22596 KiB |
| 06_bi-ternary_07.txt |
WA |
101 ms |
12728 KiB |
| 06_bi-ternary_08.txt |
WA |
71 ms |
3700 KiB |
| 06_bi-ternary_09.txt |
WA |
125 ms |
24440 KiB |
| 06_bi-ternary_10.txt |
WA |
99 ms |
9820 KiB |
| 06_bi-ternary_11.txt |
WA |
70 ms |
3760 KiB |