2018.02.14-Happy_Dog总结

  1. The Suspects
  2. How Many Answers Are Wrong
  3. Super Jumping! Jumping! Jumping!

The Suspects

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.

A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

Sample Input

1
2
3
4
5
6
7
8
9
10
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0

Sample Output

1
2
3
4
1
1

题意: 0是可能的患者,与可能的患者接触后,整个组内都是可以的患者.也就是理解为查找与0有关系的人的个数,简单的并查集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include<algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int n,m,sum,k;
int fa[30000],a[30000];

int getfather(int x)
{

if(fa[x] != x)
{
fa[x] = getfather(fa[x]);
}
return fa[x];
}
void un(int x,int y)
{
int fx = getfather(x);
int fy = getfather(y);
if(fx == fy)
return;
fa[fy] = fx;
}
int main()
{
while(cin >> n >> m &&(n || m))
{
for(int i=0;i<n;i++)
fa[i] = i;
sum = 0;
while(m--)
{
cin >> k >> a[0];
for(int i = 1;i < k; i++)
{
cin >> a[i];
un(a[0],a[i]);
}
}
for(int i = 0; i < n; i++)
if(getfather(i) == fa[0])
sum++;
printf("%d\n",sum);
}
return 0;
}

How Many Answers Are Wrong

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

1
2
3
4
5
6
10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1

Sample Output

1
1

题意: 给定几个区间的和 判断有几个错误的比如样例中 1 - 3 + 4 - 6 也就是 1 - 6 然后再加上 7 - 10 也就是 1 - 10 这时候会发现他们的和不相等.

用每个节点来记录前缀和,每次询问判断两个节点是否联通.联通就计算是否为定值不联通就合并.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define LL long long
using namespace std;

int sum[200009], fa[200009];

int getfather(int x)
{
if(fa[x] == x)
return x;
int t = fa[x];
fa[x] = getfather(fa[x]);
sum[x] += sum[t];
return fa[x];
}

void un(int x, int y, int a, int b, int c)
{
if(x > y)
{
fa[y] = x;
sum[y] = sum[a] - sum[b] - c;
}
else
{
fa[x] = y;
sum[x] = sum[b] - sum[a] + c;
}
}

int main()
{
int m, n;
while(cin >> m >> n)
{
memset(sum, 0, sizeof(sum));
for(int i = 0; i <= 200001; i++)
fa[i] = i;
int cnt = 0;
while(n--)
{
int a, b, c;
cin >> a >> b >> c;
b++;
int x = getfather(a);
int y = getfather(b);
if(x == y && sum[a] != sum[b] + c)
cnt++;
else if(x != y)
un(x, y, a, b, c);
}
printf("%d\n", cnt);
}
return 0;
}

Super Jumping! Jumping! Jumping!

Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.

The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.

Input

Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.

Output

For each case, print the maximum according to rules, and one line one case.

Sample Input

3 1 3 2
4 1 2 3 4
4 3 3 2 1
0

Sample Output

4
10
3

题意: n个数字,求最大递增子序列的和
dp 不断计算并保存当前的最大值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

int num[1001],dp[1001];

int main ()
{
int n,Max;
while (cin >> n && n)
{
Max=0;
memset(dp,0,sizeof(dp));
for (int i = 0; i < n; i++)
{
cin >> num[i];
}
dp[0] = num[0];
for (int i = 1; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (num[i] > num[j])
dp[i] = max(dp[i],dp[j] + num[i]);
}
dp[i] = max(dp[i],num[i]);
}
for (int i = 0; i < n; i++)
{
Max = max(Max,dp[i]);
}
printf ("%d\n",Max);
}
return 0;
}
script>