2019瀋陽網絡賽D題

State Z is a underwater kingdom of the Atlantic Ocean. This country is amazing. There are nn cities in the country and n-1n−1 undirected underwater roads which connect all cities.

In order to save energy and avoid traffic congestion, the king promulgated a series of traffic regulations:

  1. Residents have to travel on fish!

  2. Residents need to feed the fish before you start your trip!The amount of food you feed the fish should be exactly the total distance of your journey.

  3. What kind of food to feed depends on the total distance of your journey!Total distance is a multiple of three. You should feed the fish with Durian. Total distance modulus 33 equaling 11. It should be Papaya.Total distance modulus 33 equaling 22. It should be Avocado!!!

Sure, fish like to eat these fruits very much.

Today is the first day of the king's decree. Because the residents here are not good at mathematics, they don't know how much fruit they need to give fish to go to other cities. So the king give an order to the energy minister Ynaonlctrm From all cities to all cities directly, which means that he will make n*(n-1)n∗(n−1) trips.

For example, A - (5 mile) - B - (5 mile) - C, he needs to run the fish, starting at A, passing B, finally arriving C (papaya 10 kg), also he needs to start at C and end at A (papaya 10 kg). Indirect passage is useless. "I've passed City B, my dear emperor." "Oh! It's no use! Not directly! People in cities will never know how much the fish need to eat! The fish will surely die!!! You also need to take several trips which start at B or end with B!" The Emperor said.

It's really a tough task. Can you help him figure out how much fruit he needs to prepare for the emperor's mission?

Input

Multiple input!

Fist line is NN. next N-1N−1 line has three integer aa, bb and cc. It represent city aa and city bb is connected by a road of cc nautical miles. (1<n \le 10^41<n≤104, 0 \le a,b<n0≤a,b<n, 1 \le c<10^51≤c<105, \sum{n} \le 10^5∑n≤105)

Output

For each data, output three number, the amount of Durian, Papaya and Avocado he need. (the result could be very large, please output the result mod 10^9+7109+7)

樣例輸入1複製

5
0 1 2
0 2 3
0 3 7
0 4 6

樣例輸出1複製

54 60 30

樣例輸入2複製

8
4 7 1
7 5 1
4 6 1
6 3 1
5 2 1
2 1 1
7 0 1

樣例輸出2複製

48 54 48

題意:給出一個簡單圖,問圖中路徑長度模3餘數爲0,1,2的各個路徑長度的加和。

點分治做法

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#define ll long long
#define lld unsigned long long
#define inf 0x3f3f3f3f
#define mod 1000000007
using namespace std;
vector<pair<int,int> > g[10010];
int mx=inf;
bool vis[10010];
int son[10010],sz[10010];
ll res[10010],dis[10010],q[10010];
int sum,rt;
ll ans1,ans2,ans3;
void getroot(int x,int fa)
{
    son[x]=0;sz[x]=1;
    for(auto u: g[x])
    {
        int v=u.first;
        int w=u.second;
        if(vis[v]||v==fa) continue;
        getroot(v,x);
        sz[x]+=sz[v];
        if(sz[v]>son[x]) son[x]=sz[v];
    }
    if(sum-sz[x]>son[x]) son[x]=sum-sz[x];
    if(mx>son[x]){
        mx=son[x];
        rt=x;
    }
    return;
}
void getdis(int x,int fa,ll len)
{
    res[++res[0]]=len;
    for(auto v: g[x])
    {
        if(vis[v.first]||v.first==fa)continue;
        getdis(v.first,x,len+1ll*v.second);
    }
}
void calc(int x)
{
    for(auto v: g[x])
    {
        if(vis[v.first]) continue;
        res[0]=0;
        getdis(v.first,x,1ll*v.second);
        for(int i=1;i<=res[0];i++)
        {
            if(res[i]%3==0){//更新在同一個子樹內單鏈的貢獻
                ans1=ans1%mod+2*1ll*(res[i])%mod;
                ans1%=mod;
            }
            else if(res[i]%3==1)
            {
                ans2=ans2%mod+2*1ll*(res[i])%mod;
                ans2%=mod;
            }
            else {
                ans3=ans3%mod+2*1ll*(res[i])%mod;
                ans3%=mod;
            }
            for(int k=0;k<3;k++)
            {
                int st=(res[i]%3+k)%3;//統計和不同子樹的單鏈組合的貢獻
                if(q[k]>0)
                {
                    if(st==0){
                        ans1=ans1%mod+(2*1ll*(dis[k]+q[k]*res[i]%mod))%mod;
                        ans1%=mod;
                    }
                    else if(st==1)
                    {
                        ans2=ans2%mod+(2*1ll*(dis[k]+q[k]*res[i]%mod))%mod;
                        ans2%=mod;
                    }
                    else
                    {
                        ans3=ans3%mod+(2*1ll*(dis[k]+q[k]*res[i]%mod))%mod;
                        ans3%=mod;
                    }
                }
            }
        }
        for(int i=1;i<=res[0];i++)//將本子樹的單鏈保存
        {
            dis[res[i]%3]+=res[i];
            dis[res[i]%3]%=mod;
            q[res[i]%3]++;
            q[res[i]%3]%=mod;
        }
    }
    for(int i=0;i<3;i++)
    {
        dis[i]=q[i]=0;
    }
}
void solve(int u)
{
    rt=0;
    mx=inf;
    getroot(u,0);//找根
    vis[rt]=1;
    calc(rt);//統計
    for(auto v: g[rt])
    {
        if(vis[v.first]) continue;
        sum=sz[v.first];
        solve(v.first);
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(vis,false,sizeof vis);
        for(int i=0;i<=n;i++)
        {
            g[i].clear();
            son[i]=sz[i]=0;
        }
        ans1=ans2=ans3=0;
        for(int i=1;i<=n-1;i++)
        {
            int u,v,c;
            scanf("%d %d %d",&u,&v,&c);
            u++;v++;
            g[u].push_back(make_pair(v,c));
            g[v].push_back(make_pair(u,c));
        }
        sum=n;
        solve(1);
        printf("%lld %lld %lld\n",ans1,ans2,ans3);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章