1. 程式人生 > >1060E Sergey and Subway(樹上任意兩點之間的距離和)

1060E Sergey and Subway(樹上任意兩點之間的距離和)

Sergey and Subway

Sergey Semyonovich is a mayor of a county city N and he used to spend his days and nights in thoughts of further improvements of Nkers' lives. Unfortunately for him, anything and everything has been done already, and there are no more possible improvements he can think of during the day (he now prefers to sleep at night). However, his assistants have found a solution and they now draw an imaginary city on a paper sheet and suggest the mayor can propose its improvements.

Right now he has a map of some imaginary city with n subway stations. Some stations are directly connected with tunnels in such a way that the whole map is a tree (assistants were short on time and enthusiasm). It means that there exists exactly one simple path between each pair of station. We call a path simple if it uses each tunnel no more than once.
One of Sergey Semyonovich's favorite quality objectives is the sum of all pairwise distances between every pair of stations. The distance between two stations is the minimum possible number of tunnels on a path between them.
Sergey Semyonovich decided to add new tunnels to the subway map. In particular, he connected any two stations u and v that were not connected with a direct tunnel but share a common neighbor, i.e. there exists such a station w that the original map has a tunnel between u and w and a tunnel between w and v. You are given a task to compute the sum of pairwise distances between all pairs of stations in the new map.

Input The first line of the input contains a single integer n (2≤n≤200000) — the number of subway stations in the imaginary city drawn by mayor’s assistants. Each of the following n−1 lines contains two integers ui and vi (1≤ui,vi≤n, ui≠vi), meaning the station with these indices are connected with a direct tunnel. It is guaranteed that these n stations and n−1 tunnels form a tree. Output

Print one integer that is equal to the sum of distances between all pairs of stations after Sergey Semyonovich draws new tunnels between all pairs of stations that share a common neighbor in the original map.

Examples input 4 1 2 1 3 1 4 output 6 input 4 1 2 2 3 3 4 output 7

題意:給出一個樹,把樹上任意兩個相隔一個點的點加一條邊,問加完邊之後任意兩點的距離和是多少. 思路:51nod上有這樣一個題目,讓求樹上每個點到其他所有節點的距離和,打印出n個數,代表答案. 我們可以先一遍dfs求出每個節點到他的所有子節點的距離和,那麼根節點存的就是他到樹上每個節點的 距離和,這樣根節點的兒子就可以由他轉移過來,就可以求出題目要求的答案來. 這題也可以這樣做,稍微不同的是往下dfs也好,往下轉移也好,都是向孫子轉移,父親會由他的爺爺 dfs過來,往下轉移的時候也得向孫子.這裡的根必須是葉子節點,這樣可以避免很多問題,而根節點的兒子 需要在單獨向下轉移,因為他跟根節點都是獨立的,也可以看成根. 後面講一個簡單思路.

程式碼:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+5;

struct node
{
	int u,v,ne;
} e[maxn<<1];

int n;
int head[maxn],len;
int ve[maxn],num[maxn];
ll d[maxn];
ll ans;

void add(int u,int v)
{
	e[len].u = u;
	e[len].v = v;
	e[len].ne = head[u];
	head[u] = len++;
}

void dfs(int x,int p)
{
	ve[x] = 1;
	for(int i = head[x];~i;i = e[i].ne)
	{
		int v = e[i].v;
		if(v == p) continue;
		
		dfs(v,x);
		
		for(int j = head[v];j!= -1;j = e[j].ne)
		{
			int sv = e[j].v;
			if(sv == x) continue;
			d[x]+= d[sv]+ve[sv];//distance to grandson
		}
		d[x]++;//distance to son
		ve[x]+= ve[v];
	}
	
	return ;
}

void dfs2(int x,int p)
{
	ans+= d[x];
	for(int i = head[x];~i;i = e[i].ne)
	{
		int v = e[i].v;
		if(v == p) continue;
		
		for(int j = head[v];j!= -1;j = e[j].ne)
		{
			int sv = e[j].v;
			if(sv == x) continue;
			
			d[sv]+= d[x]-d[sv]-ve[sv]+n-ve[sv]-(ve[v]-ve[sv]);
			dfs2(sv,v);
		}
		if(p == 0)// if x is root,the son is independent
		{
			d[v]++;
			dfs2(v,x);
		}
	}
	
	return ;
}

int main()
{
	mem(head,-1);
	cin>>n;
	for(int i = 1,x,y;i< n;i++)
	{
		scanf("%d %d",&x,&y);
		add(x,y);
		add(y,x);
		num[x]++;
		num[y]++;
	}
	
	int st;
	for(int i = 1;i<= n;i++)
	{
		if(num[i] == 1)
		{
			st = i;
			break;
		}
	}
	
	dfs(st,0);
	dfs2(st,0);
	cout<<ans/2<<endl;
	
	return 0;
}

如果是求樹上任意兩點之間的距離和,我們有一個比較簡單的做法,就是不考慮點,考慮邊. 很顯然,每條邊被使用的次數是固定的,即邊的一端的節點數目乘以另一端的節點數目.這樣我們就能求 樹上任意兩點之間的距離和了,這道題就是一個變形.我們想,假如一個節點在樹上距離另一個節點偶數條邊, 那麼他是不是可以兩個兩個地跳過去,只需要走一半的路程就到了.假如有奇數條邊,兩個兩個條,最後難免 剩一條邊,其實就是(邊數+1)/2.所以,假如我們給樹規定一個層次的話,只有奇數層的節點和偶數層的節點 的路徑會出現這種狀況,其餘的都能兩步兩步跳到.所以,我們只需要把這樣的情況多+1,最後結果/2即可.

程式碼:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+5;

struct node
{
	int u,v,ne;
} e[maxn<<1];

int n;
int head[maxn],len;
int ve[maxn],b[maxn];

void add(int u,int v)
{
	e[len].u = u;
	e[len].v = v;
	e[len].ne = head[u];
	head[u] = len++;
}

void dfs(int x,int p,int k)
{
	ve[x] = 1;
	b[x] = k;
	for(int i = head[x];~i;i = e[i].ne)
	{
		int v = e[i].v;
		if(v == p) continue;
		
		dfs(v,x,k^1);
		ve[x]+= ve[v];
	}
	return ;
}

int main()
{
	mem(head,-1);
	cin>>n;
	for(int i = 1,x,y;i< n;i++)
	{
		scanf("%d %d",&x,&y);
		add(x,y);
		add(y,x);
	}
	
	dfs(1,0,0);
	
	ll ans = 0,sum = 0;
	for(int i = 1;i<= n;i++)
	{
		ans+= 1ll*ve[i]*(n-ve[i]);
		sum+= b[i];
	}
	
	ans+= sum*(n-sum);
	
	cout<<ans/2<<endl;
	
	return 0;
}