2412. Clock Tree

2412. Clock Tree

(File IO): input: clocktree.in output: clocktree.out
Time limit: 1000 ms Space limit: 262144 KB Specific limit
Goto ProblemSet


Title description

The design of Farmer John ’s new bullpen is very strange: it consists of N rooms numbered 1 ... N (2≤N≤2500), and N−1 corridors. Each corridor connects two rooms so that each room can reach any other room along some corridors.
Each room in the bullpen is equipped with a round clock with standard integers 1 ... 12 printed on the dial. However, these clocks have only one hand and always point directly to a number on the dial (it never points between two numbers).
Cow Bessie wants to synchronize all the clocks in the cowshed so that they all point to the integer 12. However, her mind is a little simpler, and when she walks in the cowshed, every time she enters a room, she moves the clock hand in the room backward by one position. For example, if the original clock pointed to 5, it will now point to 6, if the original clock pointed to 12, it will now point to 1. If Bessie enters the same room multiple times, she will turn the clock in this room every time she enters.
Request the number of possible departure rooms for Bessie so that she can walk around the bullpen and direct all the clocks to 12. Note that Bessie does not turn the clock in her departure room, but she will turn it at any time when she enters again. The clock will not move by itself; the clock will only be moved when Bessie enters. In addition, once Bessie enters a corridor, she must reach the other end of the corridor (it is not allowed to walk halfway back to the original room).

Input

The first line of input contains N. The next line contains N integers, all in the range 1 ... 12, indicating the initial clock setting for each room. Each of the following N−1 lines describes a corridor with two integers a and b, both of which are in the range 1 ... N, and are the numbers of the two rooms connected by the corridor.

Output

Outputting the number of departure rooms makes it possible for Bessie to point all clocks to 12.

Sample input

4
11 10 11 11
1 2
2 3
2 4

Sample output

1

Data range limitation
Test points 1-7 satisfy N≤100.
Test points 8-15 have no additional restrictions.

Tip
In this example, Bessie can point the clocks of all rooms to 12 (for example, move to rooms 1, 2, 3, 2, and finally to 4) if and only when she leaves room 2.

Topic idea: For
this question, we can enumerate one room (i) as the root at a time, and update the room connected to it. Since the question is to verify whether it can be completed, we may wish to look at it greedyly, and walk back and forth between father and son every time, until the child is changed to 12, at this time according to the difference between father and son, we can know what u is now. Then go to the next child and determine the value of u. Until all the children are corrected, u is fixed to a certain number at this time. Going back to the previous level, my father changed u to 12 and
then this round came down. In the end, except for the root, the other points were all 12. If the root is exactly 12, it means that it is possible. What if the root is not 12? In fact, the root is 1 because it stops at the last child from the root and does not go back. At this time, the time between the root and the last child will be one. Except for these two cases, nothing else.
if (edge ​​[i] .y == v) continue;
be sure to add this sentence, otherwise it will endlessly
because: the title stipulates that there are two-way edges between two rooms, so for any point, its son is also its father , But we stipulate that only one point should take root! !

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=5020;
struct node
{
	int x,y,next;
} edge[N];
int head[N],tot,a[N],t[N];
void add(int xx,int yy)
{
	edge[++tot].x=xx;
	edge[tot].y=yy;
	edge[tot].next=head[xx];
	head[xx]=tot;
}
void dfs(int u,int v)
{
	for(int i=head[u];i;i=edge[i].next)
	{
		if(edge[i].y==v) continue; 
		dfs(edge[i].y,u);
		t[u]=(t[u]-t[edge[i].y]+12)%12;
	}	
}
int main()
{
	//fre(clocktree);
	int n,u,v,ans=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]%=12;
	for(int i=1;i<n;i++)
	{
		scanf("%d%d",&u,&v);
		add(u,v),add(v,u);
	}
	for(int i=1;i<=n;i++)
	{
		memcpy(t,a,sizeof(t));
		dfs(i,0);
		if(!t[i]||t[i]==1) ans++;
	}
	printf("%d\n",ans);
	return 0;
}

Published 130 original articles · won 93 · views 6802

Guess you like

Origin blog.csdn.net/bigwinner888/article/details/105347965