Openjudge 百练第4109题

 1 # -*- coding:utf-8 -*-
 2
 3 def set_1(i, q):
 4     ‘‘‘ generate a i*i ARRAY for all relationships
 5     if there is a relation set 1 or 0
 6     return i*i ARRAY with 1 or 0
 7     ‘‘‘
 8     a = [0 for i in range(i*i)]
 9     for j in range(len(q)):
10         n, m = q[j]
11         a[(n-1)*i+(m-1)] = 1
12         a[(m-1)*i+(n-1)] = 1
13     return a
14
15 def solve(i, q, r):
16     ‘‘‘ solve question
17     i is the number of people
18     q is the set of questions
19     r is the set of relationships, the result of function set_1();
20     ‘‘‘
21     result = 0
22     for j in range(len(r)):
23         n, m = r[j]
24         for l in range(i):
25             if q[(n-1)*i+l] == 1 and q[(m-1)*i+l] == 1:
26                 result += 1
27         print(result)
28         result = 0
29
30 def main():
31     d = [ 3, [3,2,2],
32              [1,3],
33              [2,3],
34              [1,2],
35              [1,3],
36              [4,3,2],
37              [1,2],
38              [2,3],
39              [1,4],
40              [2,4],
41              [1,3],
42              [5,2,1],
43              [1,2],
44              [1,4],
45              [3,4]
46         ]
47     for x in d:                         #Dispaly input
48         print(x)
49
50     loc = []
51     for m in range(1,len(d)):           #Get the index of every question
52         if len(d[m])==3:
53             loc.append(m)
54
55     for i in range(len(loc)):           #Sovle each question
56         pNum, qNum, aNum = d[loc[i]]    #slice out R and Q in d[]
57         t = loc[i]+1
58         R = d[t:t+qNum]
59         Q = d[t+qNum:t+qNum+aNum]
60
61         r_1 = set_1(pNum,R)             # set 1 for question
62         print(‘-------------------\nCase‘+str(i+1)+‘:‘)
63         solve(pNum, r_1, Q)
64
65 if __name__==‘__main__‘:
66     main()
67
68 ‘‘‘OUTPUT
69 3
70 [3, 2, 2]
71 [1, 3]
72 [2, 3]
73 [1, 2]
74 [1, 3]
75 [4, 3, 2]
76 [1, 2]
77 [2, 3]
78 [1, 4]
79 [2, 4]
80 [1, 3]
81 [5, 2, 1]
82 [1, 2]
83 [1, 4]
84 [3, 4]
85 -------------------
86 Case1:
87 1
88 0
89 -------------------
90 Case2:
91 1
92 1
93 -------------------
94 Case3:
95 0
96 ‘‘‘

OpenJudge看到一个题目:

描述

小明和小红去参加party。会场中总共有n个人,这些人中有的是朋友关系,有的则相互不认识。朋友关系是相互的,即如果A是B的朋友,那么B也是A的朋友。小明和小红想知道其中某两个人有多少个公共的朋友。

输入第一行为一个正整数c,代表测试数据的个数。接下来是c组测试数据。

对于每组测试数据,第一行是三个数字n(2<=n<=100),m和k,分别表示会场中的人数,已知的朋友关系数目,问题的数目。接下来的m行,每行用两个数字i和j(1<=i,j<=n)表示了一个朋友关系,表示第i个人和第j个人是朋友关系。接下来的k行,每行用两个数字i和j(1<=i,j<=n)表示一个问题,请问第i个人和第j个人有多少公共的朋友。输出对于第i组测试数据,首先输出一行”Case i:”,接下来得k行代表了k个问题,每行输出第i个人和第j个人有多少公共的朋友。

时间: 2024-11-12 06:23:57

Openjudge 百练第4109题的相关文章

[OpenJudge] 百练2754 八皇后

八皇后 Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2...b8,其中bi为相应摆法中第i行皇后所处的列数.已经知道8皇后问题一共有92组解(即92个不同的皇后串).给出一个数b,要求输出第b个串.串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小. I

Poj OpenJudge 百练 1860 Currency Exchang

1.Link: http://poj.org/problem?id=1860 http://bailian.openjudge.cn/practice/1860 2.Content: Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20706   Accepted: 7428 Description Several currency exchange points are working

Poj OpenJudge 百练 1062 昂贵的聘礼

1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37631   Accepted: 10872 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金

Poj OpenJudge 百练 1573 Robot Motion

1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10856   Accepted: 5260 Description A robot has been programmed to follow the instru

Poj OpenJudge 百练 2632 Crashing Robots

1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7912   Accepted: 3441 Description In a modernized warehouse, robots are used to

Openjudge 百练 / 2524 - 宗教信仰 [并查集]

URL: http://bailian.openjudge.cn/practice/2524/ [描述] 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 50000),你不太可能询问每个人的宗教信仰,因为他们不太愿意透露.但是当你同时找到2名学生,他们却愿意告诉你他们是否信仰同一宗教,你可以通过很多这样的询问估算学校里的宗教数目的上限.你可以认为每名学生只会信仰最多一种宗教. [输入] 输入包括多组数据. 每组数据的第一行包括n和m,0 &l

Poj OpenJudge 百练 2602 Superlong sums

1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlong sums Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22337   Accepted: 6577 Description The creators of a new programming language D++

Poj OpenJudge 百练 2389 Bull Math

1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13067   Accepted: 6736 Description Bulls are so much better at math than the cows. The

Poj OpenJudge 百练 Bailian 1008 Maya Calendar

1.Link: http://poj.org/problem?id=1008 http://bailian.openjudge.cn/practice/1008/ 2.content: Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66971   Accepted: 20644 Description During his last sabbatical, professor M. A. Ya