Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

牛客上过了,poj哇了,不知道哪里卡了

Posted by Fadeeye at 2020-07-31 17:31:27 on Problem 3417
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector> 
// #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sp ' '
#define endl '\n'
#define FOR(i,a,b) for( int i = a;i <= b;++i)
#define bug cout<<"--------------"<<endl
#define P pair<int, int>
#define fi first
#define se second
#define pb(x) push_back(x)
#define ppb() pop_back()
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define sca2(a,b) scanf("%d %d",&(a),&(b))
#define sca(a) scanf("%d",&(a));
#define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c))
#define sca2ll(a,b) scanf("%lld %lld",&(a),&(b))
#define scall(a) scanf("%lld",&(a));


using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;}

const double Pi = acos(-1.0);
const double epsilon = Pi/180.0;
const int maxn = 7e5+10;
int tot = 0;
int head[maxn],ver[maxn],edge[maxn],nexts[maxn];
void add(int x,int y,int z)
{
    ver[++tot] = y,edge[tot] = z,nexts[tot] = head[x] , head[x] = tot;
}
void add(int x,int y){
    ver[++tot] = y;
    nexts[tot] = head[x];
    head[x] = tot;
    //edge[tot] = z;
}
int k,n,m;
ll d[maxn],f[maxn][30],vis[maxn],cf[maxn];
ll sum[maxn];

void bfs(){
    queue<int> que;
    que.push(1);
    d[1] = 1;
    while(!que.empty()){
        int x = que.front();
        que.pop();
        for(int i = head[x]; i; i = nexts[i]){
            int y = ver[i];
            if(d[y]) continue;
            //dist[y] = dist[x] + edge[i];
            d[y] = d[x] + 1;
            f[y][0] = x;
            for(int j = 1; j <= k;j++){
                f[y][j] = f[f[y][j-1]][j-1];
            }
            que.push(y);
        }
    }
}
int lca(int x, int y){
    if(d[x] > d[y]) swap(x,y);   //设y比较深,深度d大
    for(int i = k;i >= 0;i--){
        if(d[f[y][i]] >= d[x]) y = f[y][i];
    }
    if(x == y) return x;
    for(int i = k;i >= 0;i--){
        if(f[x][i] != f[y][i]){
            x = f[x][i];
            y = f[y][i];
        }
    }
    return f[x][0];
}
ll dfs(int x)
{
    ll cnt = 0;
    for(int i = head[x];i;i = nexts[i]){
        int y = ver[i];
        if(vis[y] == 1) continue;
        vis[y] = 1;
        cnt +=  dfs(y);
    }
    return sum[x] = cnt+cf[x];
}
int main()
{
    //freopen("input.txt", "r", stdin);
    sca2(n,m);
    //cin>>n>>m;
    rep(i,1,n-1){
        int x,y;
        sca2(x,y);
        //cin>>x>>y;
        add(x,y);
    }
    k = (int)(log(n) / log(2)) + 1;
    bfs();
    rep(i,1,m){
        int x,y;
        sca2(x,y);
        cf[x]++;
        cf[y]++;
        cf[lca(x,y)] -= 2;
    }
    vis[1] = 1;
    dfs(1);
    ll ans = 0;
    rep(i,2,n){
        if(sum[i] == 0) ans += m;
        else if(sum[i] == 1) ans ++;
    }
    printf("%lld\n",ans );
    //cout<<ans<<endl;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator