Cattle passenger Challenge 36 A. ring (mathematics, there is the ring)

Topic links: Click here
Here Insert Picture Description
Here Insert Picture Description
meaning of the questions it wrong, has been 60%.

Ideas:

Set point on the ring are numbered as pi, of each side length were Vi.

WP1 + WP2 = V1 - ①

Wp2+Wp3=V2——②

Wp3 + Wp4 = V3 - ③

……

Wpn Wp1 = MT (n)

①-② + ③ ... + (n), to give 2Wp1 = V1-V2 + V3- ... + Vn.

Solved Wp1 then in turn introduced Wpi.

note:

Vertex numbers in order to not to be considered as a directed ring.

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;

int Next[maxn], weight[maxn], ans[maxn];

int main()
{
	int n, x, y, z;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)		//输入n条边,构成有向环 
    {
		scanf("%d%d%d", &x, &y, &z);
        if(Next[x]!=0)	swap(x, y);
        Next[x] = y;
		weight[x] = z;
    }
    
    int j = 1;	//起点从1开始 
    int sum = 0;
    for(int i = 1; i <= n; i++)		//共n条边,故循环n次 
    {
    	//二进制最末位为0表示偶数,最末位为1表示奇数
    	if(i&1)	sum += weight[j];
		else	sum -= weight[j];
		j = Next[j];
	}
	
	//左移1位就是乘以2,右移1位就是除以2
    ans[1] = sum>>1;
    
    j = 1;
    for(int i = 2; i <= n; i++)		//剩余n-1个点,故循环n-1次 
    {
    	ans[Next[j]] = weight[j] - ans[j];
		j = Next[j];
	}
    
    for(int i = 1; i <= n; i++)
		printf("%d\n", ans[i]);
    return 0;
}
Published 673 original articles · won praise 103 · views 110 000 +

Guess you like

Origin blog.csdn.net/qq_42815188/article/details/104033467