[HNOI2005] cunning businessman

Diao Cha received a mission to investigate a businessman's books for the tax department, look at the books is not counterfeit. Recorded on the books since the income situation of n-month
regime in which income amounted to i-th month of Ai (i = 1,2,3 ... n- 1, n),. This month represents earnings element Ai, Ai indicates when Ai is less than 0 is greater than 0 when
the month loss element Ai. Total revenue in the so-called period of time, that is, the sum of the amount of income each month during that time. Diao Cha task is carried out in secret
, in order to investigate businessmen books, she had to go there to work businessman. She businessmen to take advantage of absence to peek at the books, but she could not steal the books, every time
she can only see the income recorded on the books within a certain period of time when peeking books, and she could only remember this time of total revenue. Now, Diao Cha overall
total peep m times the books, of course, remember the total revenue in the m period of time, your task is to remember this information to determine the books is not a fake.
Input
first line a positive integer w, where w <100, w represents a set of data has, i.e. a w books, you need determination.
The first row of each data two positive integers n and m, where n <100, m <1000,
respectively, corresponding to the record books many months income and how many books peek.
The next line m represents the m information Mateo Cha remember books peek m times,
each piece of information per line, there are three integers s, t and v, represents from s to t-month month (including the first t months) total income v,
where s is always less than or equal t is assumed.
Output
contains w lines, each is true or false,
wherein the i-th behavior true if and only if the i-th group of data, i.e., the i-th books not false; i-th row is false if and only if the i-th group of data, i.e., the first i-books is false.
The Input the Sample
2
3 3
1 2 10
1 3 -5
3 3 -15
5 3
1 5 100
3 5 50
1 2 51
Sample Output
true
false

 

Sol: this question we can define a portion of the tree and out. I.e., dist [i] is equal to the distance from the root node to the communication block i this point. This will be the relationship between all of the month, organized into a tree form. You can then be quickly know [i, j] from the value of this paragraph. That how to maintain it?

Maintenance is divided into two parts

1: communicating the interior of the block at a point, i.e. a point in the query, the point may be obtained recursively from the root to achieve the following:

int get(int x) 
// realize the communication block a point, the distance to the root node 
{
    if(fa[x]==x)return x;
    int y = does [x];
    for [x] = get (for [x]);
    say [x] + tell [them];
    return fa[x];
}

  

2: When merging two connected blocks, only this time a large number of the root node is calculated with a smaller number of distance to the root node. Note that the smaller number of the root node must let combined as after the root block communication. To achieve the following:

bool merge(int x,int y,int z)
{
    int fx=get(x),fy=get(y);
    if(fx==fy)return dis[y]-dis[x]==z;
    if (fx <fy) // when the two blocks to be merged when the communication 
	    fa [fy] = fx, say [fy] = Dis [x] + z-dis [y];
	    // note that here is an update y father points to a new ancestor fy fx point distance 
    else 
		fa [fx] = fy, tell [fx] = say [y] z say [x];
    return 1;
}

 The complete code is as follows:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 106
using namespace std;
inline char nc(){
    static char buf[100000],*i=buf,*j=buf;
    return i==j&&(j=(i=buf)+fread(buf,1,100000,stdin),i==j)?EOF:*i++;
}
inline int _read(){
    char ch=nc();int sum=0,p=1;
    while(ch!='-'&&!(ch>='0'&&ch<='9'))ch=nc();
    if(ch=='-')p=-1,ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum*p;
}
T int n, m ago [maxn] dis [maxn];
int get(int x) 
// realize the communication block a point, the distance to the root node 
{
    if(fa[x]==x)return x;
    int y = does [x];
    for [x] = get (for [x]);
    say [x] + tell [them];
    return fa[x];
}
bool merge(int x,int y,int z)
{
    int fx=get(x),fy=get(y);
    if(fx==fy)return dis[y]-dis[x]==z;
    if (fx <fy) // when the two blocks to be merged when the communication 
	    fa [fy] = fx, say [fy] = Dis [x] + z-dis [y];
	    // note that here is an update y father points to a new ancestor fy fx point distance 
    else 
		fa [fx] = fy, tell [fx] = say [y] z say [x];
    return 1;
}
int main () {
  
    T=_read();
    while(T--){
        n=_read();m=_read();bool ans=1;
        for(int i=0;i<=n;i++)
		four [c] = on, dis [c] = 0;
        for(int i=1;i<=m;i++){
            int x=_read(),y=_read(),z=_read();
            if(!merge(x-1,y,z))ans=0;
        }
        if(ans)printf("true\n");
          else printf("false\n");
    }
    return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/cutemush/p/12457419.html