[Ox-off] [# 1108 J] [tree] buy one get one

2019 cattle off the National Day party day3 training

Links: https://ac.nowcoder.com/acm/contest/1108/J
Source: Cattle-off network

The meaning of problems

ICPCCamp there are n shops, with $ 1,2, ..., n $  numbers. For any i> 1, there are $ p_i $ from the store to the one-way road of i. Meanwhile, the shop i sell $ a_i $ types of merchandise. Bobo depart from the store 1 store i. He wants to buy goods in two different shops (including shops 1 and i). He first set up the type of goods purchased is x, after buying a commodity type y, he used $ f_i $ express different on $ \ langle x, y \ rangle $ orderly  number. Obtained $ f_2, f_3, \ dots, f_n $ value.


Thinking

Since there is only one point of each penetration, it can be converted to a root of a tree structure.

The question then is converted to, a different number of ordered pairs on the path from the root to $ I $ $ 1 to $.

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize(4)
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>
//#include <unordered_set>
//#include <unordered_map>
// #include<bits/extc++.h>
// using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}

/**********showtime************/
            const int maxn = 1e5+9;
            int p[maxn], a[maxn];
            ll f[maxn];
            int sum,kind[maxn],cnt[maxn];
            vector<int>mp[maxn];
            
            ll add(int col) {
                ll res = 0;
                res = sum - kind[col];
                if(cnt[col] == 1) res ++;
                else if(cnt[col] == 0) sum ++;
                cnt[col] ++;
                kind[col] = sum;
                return res;
            }
            void del(int col) {
                cnt[col] --;
                if(cnt[col] == 0) sum--;
            }
            
            void dfs(int u, int fa, int dp) {
                f[u] = f[fa];
                int yk = kind[a[u]];
                f[u] += add(a[u]);

                for(int v : mp[u]) {
                    dfs(v, u, dp+1);
                }

                del(a[u]);

                kind[a[u]] = yk;
            }
int main(){
            int n;
            while(~scanf("%d", &n)) {

                for(int i=1; i<=n; i++) mp[i].clear(), f[i] = 0, kind[i] = 0;
                sum = 0;

                for(int i=2; i<=n; i++) {
                    scanf("%d", &p[i]);
                    mp[p[i]].pb(i);
                }
                for(int i=1; i<=n; i++) scanf("%d", &a[i]);

                dfs(1, 1, 1);

                for(int i=2; i<=n; i++) printf("%lld\n", f[i]);
            }
            return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/ckxkexing/p/11621858.html
one
ONE