7.27T2

Do not question

sol: First, there is a very obvious nature of the answer must be the best in the leaves, and then painting map is found from the bottom to see if it is seen as one strand of a point up on a non-chain node, its son number to -1 answers contribution, all this add up is the answer

Very easy to achieve, write an array bo [i] if i is represented chain, XJB make trouble on it

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0; bool f=0; char ch=' ';
    while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
    while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0) {putchar('-'); x=-x;}
    if(x<10) {putchar(x+'0'); return;}
    write(x/10); putchar((x%10)+'0');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=1000005,M=2000005;
int n,ans=0;
int deg[N],tot=0,Next[M],to[M],head[N];
bool bo[N];
inline void Link(int x,int y)
{
    Next[++tot]=head[x]; to[tot]=y; head[x]=tot; deg[y]++;
}
inline void dfs(int x,int fat)
{
//    cout<<"x="<<x<<' '<<"fat="<<fat<<endl;
    int e,sum=0;
    bool flg=1;
    bo[x]=1;
    for(e=head[x];e;e=Next[e]) if(to[e]!=fat)
    {
        if(flg)flg=0; else bo[x]=0;
        dfs(to[e],x);
        if(!bo[to[e]])bo[x]=0; else sum++;
    }
    if(sum>0) ans+=sum-1;
}
int main()
{
    freopen("iiiiiii.in","r",stdin);
    freopen("iiiiiii.out","w",stdout);
    int i,x,y;
    R(n);
    if(n==1) return puts("0"),0;
    for(i=1;i<n;i++)
    {
        R(x); R(y); Link(x,y); Link(y,x);
    }
    for(i=1;i<=n;i++) if(deg[i]>2) {dfs(i,0); Wl(ans); return 0;}
    puts("1");
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/gaojunonly1/p/11256550.html