就说两比较有代表性的题。
食物链 POJ 1182
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。
现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。
有人用两种说法对这N个动物所构成的食物链关系进行描述:
第一种说法是"1 X Y",表示X和Y是同类。
第二种说法是"2 X Y",表示X吃Y。
此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。
1) 当前的话与前面的某些真的话冲突,就是假话;
2) 当前的话中X或Y比N大,就是假话;
3) 当前的话表示X吃X,就是假话。
你的任务是根据给定的N(1 <= N <= 50,000)和K句话(0 <= K <= 100,000),输出假话的总数。
Input
第一行是两个整数N和K,以一个空格分隔。
以下K行每行是三个正整数 D,X,Y,两数之间用一个空格隔开,其中D表示说法的种类。
若D=1,则表示X和Y是同类。
若D=2,则表示X吃Y。
Output
只有一个整数,表示假话的数目。
Sample Input
100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5
Sample Output
3
#include <stdio.h>
#include <stdlib.h>
int pre[100005], rela[100005];
int find(int x)
{
if(x==pre[x]) return x;
else
{
int t=pre[x];
pre[x] = find(pre[x]);
rela[x] = (rela[x]+rela[t]) % 3;
return pre[x];
}
}
int main()
{
int n, k, i, d, x, y, fx, fy;
int re;
re = 0;
scanf("%d %d", &n, &k);
for(i=1;i<=k;i++)
{
pre[i] = i;
}
while(k--)
{
scanf("%d %d %d", &d, &x, &y);
if((x==y&&d==2)||x>n||y>n) re++;
else
{
fx = find(x);
fy = find(y);
if(fx==fy)
{
if(d-1!=(rela[x]-rela[y]+3)%3) re++;
}
else
{
pre[fx] = fy;
rela[fx] = (rela[y] - rela[x] + d - 1 )%3;
}
}
}
printf("%d\n", re);
return 0;
}
How Many Answers Are Wrong HDU 3088
TT and FF are … friends. Uh… very very good friends -________-b
FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).
Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF’s question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.
BoringBoringa very very boring game!!! TT doesn’t want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.
The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.
However, TT is a nice and lovely girl. She doesn’t have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.
What’s more, if FF finds an answer to be wrong, he will ignore it when judging next answers.
But there will be so many questions that poor FF can’t make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)
Input
Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.
Line 2…M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It’s guaranteed that 0 < Ai <= Bi <= N.
You can assume that any sum of subsequence is fit in 32-bit integer.
Output
A single line with a integer denotes how many answers are wrong.
Sample Input
10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1
Sample Output
1
题意大概就是,有一堆数,已知多组从谁到谁这之间的所有数的和。让判断他所说的有多少是错的
#include <stdio.h>
#include <string.h>
#include <math.h>
long long pre[200000], rela[200000];
long long re;
long long find(long long x)
{
if(x!=pre[x])
{
int t = pre[x];
pre[x] = find(pre[x]);
rela[x] += rela[t];
return pre[x];
}
else
{
return x;
}
}
void unite(long long u, long long v, long long w)
{
int fu = find(u);
int fv = find(v);
if(fu!=fv)
{
pre[fv] = fu;
rela[fv] = rela[u] - rela[v] + w;
}
else
{
if(w!=rela[v] - rela[u]) re++;
}
}
int main()
{
long long n, m, i, u, v, w;
while(~scanf("%d %d", &n, &m))
{
re = 0;
for(i=1; i<=n; i++)
{
pre[i] = i;
rela[i] = 0;
}
while(m--)
{
scanf("%lld %lld %lld", &u, &v, &w);
u--;
unite(u, v, w);
}
printf("%lld\n", re);
}
return 0;
}