传送门
文章目录
- 题意:
- 思路:
题意:
思路:
首先考虑最小值,如果从一个叶子结点出发到任意叶子的距离都为偶数,那么只需要一个值就可以满足条件。如果有奇数的,考虑111 ^ 222 ^ 3=03=03=0,我们可以在非连接叶节点的边上交错填1,21,21,2,在叶节点的边上填333,这样就可以保证满足条件。所以答案为111或333。
考虑最大值,由于我们填的数可以无限大, 所以考虑对于每个边都先分配一个不同的值,假设有kkk个叶子连接在一个点上,那么我们需要减去k−1k-1k−1,因为这些点的边都需要填一样的值。
这个过程可以直接求,也可以dpdpdp来求。
复杂度O(N)O(N)O(N)。
直接求:
// Problem: D. Edge Weight Assignment
// Contest: Codeforces - Codeforces Round #633 (Div. 2)
// URL: https://codeforces.com/contest/1339/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
int d[N],cd[N],a[N];
int f[N];
vector<int>v[N];
bool flag;void dfs1(int u,int fa,int d) {for(auto x:v[u]) {if(x==fa) continue;dfs1(x,u,d+1);}if(v[u].size()==1) {if(d%2==1) flag=1;}
}void dfs2(int u,int fa) {int add=0;for(auto x:v[u]) {if(x==fa) continue;dfs2(x,u);if(v[x].size()==1) add=1;else f[u]+=f[x]+1;}f[u]+=add;
}int solve1() {for(int i=1;i<=n;i++) if(d[i]==1) { dfs1(i,0,0); break; }if(flag) return 3;else return 1;
}int solve2() {int ans=n-1;for(int i=1;i<=n;i++) {if(d[i]==1) continue; int cnt=0;for(auto x:v[i]) {if(v[x].size()==1) cnt++;}ans-=max(0,cnt-1);}return ans;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);scanf("%d",&n);for(int i=1;i<=n-1;i++) {int a,b; scanf("%d%d",&a,&b);v[a].pb(b); v[b].pb(a);d[a]++; d[b]++;}printf("%d %d\n",solve1(),solve2());return 0;
}
/**/
树形dpdpdp
// Problem: D. Edge Weight Assignment
// Contest: Codeforces - Codeforces Round #633 (Div. 2)
// URL: https://codeforces.com/contest/1339/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
int d[N],cd[N],a[N];
int f[N];
vector<int>v[N];
bool flag;void dfs1(int u,int fa,int d) {for(auto x:v[u]) {if(x==fa) continue;dfs1(x,u,d+1);}if(v[u].size()==1) {if(d%2==1) flag=1;}
}void dfs2(int u,int fa) {int add=0;for(auto x:v[u]) {if(x==fa) continue;dfs2(x,u);if(v[x].size()==1) add=1;else f[u]+=f[x]+1;}f[u]+=add;
}int solve1() {for(int i=1;i<=n;i++) if(d[i]==1) { dfs1(i,0,0); break; }if(flag) return 3;else return 1;
}int solve2() {for(int i=1;i<=n;i++) {if(d[i]>=2) {dfs2(i,0);return f[i];}}return 0;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);scanf("%d",&n);for(int i=1;i<=n-1;i++) {int a,b; scanf("%d%d",&a,&b);v[a].pb(b); v[b].pb(a);d[a]++; d[b]++;}printf("%d %d\n",solve1(),solve2());return 0;
}
/**/