[Bzoj]4690:never wait for Weights with the right and check set of __ and search

Source: Internet
Author: User
Tags first row
DescriptionIn the lab, Nathan Wada's role as assistant was to determine the weight differences of two samples. When the sample difference is very small, using the balance can be more accurate than using spring balance, so he only uses the balance to measure some samples of the weight difference. He is occasionally asked about the weight of some samples, and whether he can answer these questions depends on the measurements he has obtained when answering the questions. Because the measurements he's working on are huge, he wants you to write a program to help him with the data and answer the questions.

Input Input contains multiple sets of test data. The first row of each set of data contains two integers n and M, where n indicates the number of samples, samples from 1 to N, and satisfies 2≤n≤100000. Next line M, each row includes a measurement result or query, given in chronological order, satisfying 1≤m≤100000. A measurement result is formatted! A B W, indicating that the first a sample is lighter than the first B sample light W unit weight to meet a≠b,0≤w≤1000000, and arbitrary test results are not contradictory. A query is formatted? A b indicates how many units of weight the sample a sample is lighter than the first B, which satisfies the a≠b. The input ends with two 0.

output a row for each query, if you can answer the question, the output of the answer, you can think that the absolute value of the answer is not more than 1000000 otherwise output UNKNOWN, indicating that can not answer the question.

The first to take right and check the collection of the topic, but also quite understood, pay attention to the details, more review ... Code:

#include <cstdio>
#include <cstring>
const int maxn=100005;
int F[MAXN],D[MAXN];
int Findfa (int x)
{
	if (f[x]==x) return x;
	D[X]+=D[F[X]];
	int oldfa=f[x];
	F[X]=FINDFA (F[x]);
	D[X]+=D[OLDFA];
	return f[x];
}
void work (int x,int y,int z)
{
	int fx=findfa (x), Fy=findfa (y);
	if (fx==fy) return;
	F[fx]=fy;
	D[FX]=D[Y]+Z-D[X];
}
int main ()
{
	int n,m;
	while (1)
	{
		scanf ("%d%d", &n,&m);
		if (n==0 && m==0) break;
		for (int i=1;i<=n;i++) f[i]=i,d[i]=0;
		for (int i=1;i<=m;i++)
		{
			char s[3];
			int x,y,z;
			scanf ("%s%d%d", s,&x,&y);
			if (s[0]== '! ')
			{
				scanf ("%d", &z);
				Work (x,y,z);
			}
			else 
			{
				int fx=findfa (x), Fy=findfa (y);
				if (fx!=fy) printf ("unknown\n");
				else printf ("%d\n", D[x]-d[y]);}}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.