結果
| 問題 |
No.1002 Twotone
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-10-10 21:46:40 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,239 bytes |
| コンパイル時間 | 2,278 ms |
| コンパイル使用メモリ | 196,308 KB |
| 実行使用メモリ | 62,640 KB |
| 最終ジャッジ日時 | 2024-09-13 01:10:37 |
| 合計ジャッジ時間 | 16,650 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 24 WA * 9 |
コンパイルメッセージ
main.cpp: In lambda function:
main.cpp:40:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
40 | for(auto[nx,_]:g[x]){
| ^
main.cpp: In lambda function:
main.cpp:65:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
65 | for(auto[cx,cy]:g[x]){
| ^
main.cpp: In lambda function:
main.cpp:100:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
100 | for(auto[cx,cy]:g[cen[0]]){
| ^
main.cpp:109:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
109 | for(auto[CX,CY]:CA)A[CX]+=CY;
| ^
main.cpp:110:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
110 | for(auto[CX,CY]:CB)B[CX]+=CY;
| ^
main.cpp:111:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
111 | for(auto[CX,CY]:CC)C[CX]+=CY;
| ^
main.cpp:117:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
117 | for(auto[CX,CY]:memo){
| ^
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (ll i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vec;
using Graph = vector<vector<ll>>;
const long long INF = 1LL<<60;
const long long MOD = 1000000007;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll n,k;
cin>>n>>k;
vector<vector<P>> g(n);
rep(i,n-1){
ll a,b,c;
cin>>a>>b>>c;
a--;b--;
g[a].push_back({b,c});
g[b].push_back({a,c});
}
vec dead(n);
vec par(n);
vec sub(n);
vec cen;
ll ans=0;
map<ll,ll> A,B;
map<P,ll> C;
ll cnt=0;
function<ll(ll,ll,ll)> dfs=[&](ll x,ll p,ll size){
sub[x]=1;
bool flg=1;
for(auto[nx,_]:g[x]){
if(dead[nx])continue;
if(nx==p)continue;
par[nx]=x;
sub[x]+=dfs(nx,x,size);
if(sub[nx]>size/2)flg=0;
}
if(size-sub[x]>size/2)flg=0;
if(flg)cen.push_back(x);
return sub[x];
};
function<void(ll,ll)> efs=[&](ll x,ll size){
vector<P> memo;
cen.clear();
dfs(x,-1,size);
assert(!cen.empty());
dead[cen[0]]=1;
A.clear();
B.clear();
C.clear();
cnt=0;
map<ll,ll> CA,CB;
map<P,ll> CC;
ll ccnt=0;
function<void(ll,ll,P)> ffs=[&](ll x,ll p,P id){
for(auto[cx,cy]:g[x]){
if(cx==p)continue;
if(dead[cx])continue;
P cid=id;
if(id.first==-1&&id.second!=cy){
cid.first=cy;
ans++;
if(cid.first>cid.second)swap(cid.first,cid.second);
ans+=C[cid];
ans+=A[cid.first];
ans+=A[cid.second];
CC[cid]++;
CB[cid.first]++;
CB[cid.second]++;
ffs(cx,x,cid);
}
else if(id.first==-1&&id.second==cy){
ans+=B[id.second];
ans+=cnt-A[id.second];
CA[id.second]++;
ccnt++;
ffs(cx,x,id);
}
else if(id.first!=-1&&(id.first==cy||id.second==cy)){
ans++;
ans+=C[id];
ans+=A[id.first];
ans+=A[id.second];
CC[id]++;
CB[id.first]++;
CB[id.second]++;
ffs(cx,x,id);
}
}
};
for(auto[cx,cy]:g[cen[0]]){
if(dead[cx])continue;
if(cx==par[cen[0]]){
memo.push_back({cx,size-sub[cen[0]]});
}
else memo.push_back({cx,sub[cx]});
ans+=B[cy];
ans+=cnt-A[cy];
ffs(cx,cen[0],{-1,cy});
for(auto[CX,CY]:CA)A[CX]+=CY;
for(auto[CX,CY]:CB)B[CX]+=CY;
for(auto[CX,CY]:CC)C[CX]+=CY;
cnt+=ccnt;
cnt++;
A[cy]++;
CA.clear();CB.clear();CC.clear();ccnt=0;
}
for(auto[CX,CY]:memo){
if(CY>=1)efs(CX,CY);
}
};
efs(0,n);
cout<<ans<<endl;
return 0;
}