0%

ABC-397-E - Path Decomposition of a Tree

思路讲解

总的来说就是问你这颗树能不能分解成由K个点组成的路径

哈哈,我的理解有问题,其实这个所谓的“路径”是“链”(根据形式化题意)

AC代码

https://atcoder.jp/contests/abc397/submissions/64463352

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
// Problem: E - Path Decomposition of a Tree
// Contest: AtCoder - OMRON Corporation Programming Contest 2025 (AtCoder Beginner Contest 397)
// URL: https://atcoder.jp/contests/abc397/tasks/abc397_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
typedef pair<ll,bool> plb;
constexpr ll MAXN=static_cast<ll>(2e5)+10,INF=static_cast<ll>(5e18)+3;

ll N,K,T;
vector<ll> g[MAXN];
bool vis[MAXN];
bool Ans=true;
// bool needed[MAXN];

ll dfs(ll x){ // 如果return 0,就说明子树已经自行满足了,其他数就说明还需要K-x个点
if(!Ans) return 0;
vis[x]=true;
// vector<ll> rec;
ll sum=0;
ll cnt=0;
for(int i=0;i<g[x].size();++i){
ll node=g[x][i];
if(vis[node]) continue;
ll lrec=dfs(node);
if(lrec!=0) ++cnt;
// rec.pb(lrec);
sum+=lrec;
}
if(sum+1==K && cnt<=2){
return 0;
}
if(sum+1<K && cnt<=1){
return sum+1;
}
Ans=false;
return 0;
}

inline void solve(){
cin>>N>>K;
if(K==1){
cout<<"Yes\n";
return;
}
FOR(i,1,N*K-1){
ll u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
Ans=true;
dfs(1);
if(Ans) cout<<"Yes\n";
else cout<<"No\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// cin>>T;
// while(T--){
// solve();
// }
solve();
return 0;
}
/*
AC
https://atcoder.jp/contests/abc397/submissions/64463352
*/

心路历程(WA,TLE,MLE……)

一个是没有注意到链

还有这个条件是cnt≤1

1
2
3
if(sum+1<K && cnt<=1){
return sum+1;
}