Openjudge 百练第4109题
在OpenJudge看到一个题目(#4109),题目描述如下:
小明和小红去参加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个人有多少公共的朋友。
用Python写了段代码,大致实现:
# -*- coding:utf-8 -*- def set_1(i, q):
''' generate a i*i ARRAY for all relationships
if there is a relation set 1 or 0
return i*i ARRAY with 1 or 0
'''
a = [0 for i in range(i*i)]
for j in range(len(q)):
n, m = q[j]
a[(n-1)*i+(m-1)] = 1
a[(m-1)*i+(n-1)] = 1
return a def solve(i, q, r):
''' solve question
i is the number of people
q is the set of questions
r is the set of relationships, the result of function set_1();
'''
result = 0
for j in range(len(r)):
n, m = r[j]
for l in range(i):
if q[(n-1)*i+l] == 1 and q[(m-1)*i+l] == 1:
result += 1
print(result)
result = 0 def main():
d = [ 3, [3,2,2],
[1,3],
[2,3],
[1,2],
[1,3],
[4,3,2],
[1,2],
[2,3],
[1,4],
[2,4],
[1,3],
[5,2,1],
[1,2],
[1,4],
[3,4]
]
for x in d: #Dispaly input
print(x) loc = []
for m in range(1,len(d)): #Get the index of every question
if len(d[m])==3:
loc.append(m) for i in range(len(loc)): #Sovle each question
pNum, qNum, aNum = d[loc[i]] #slice out R and Q in d[]
t = loc[i]+1
R = d[t:t+qNum]
Q = d[t+qNum:t+qNum+aNum] r_1 = set_1(pNum,R) # set 1 for question
print('-------------------\nCase'+str(i+1)+':')
solve(pNum, r_1, Q)
if __name__=='__main__':
main() '''OUTPUT
3
[3, 2, 2]
[1, 3]
[2, 3]
[1, 2]
[1, 3]
[4, 3, 2]
[1, 2]
[2, 3]
[1, 4]
[2, 4]
[1, 3]
[5, 2, 1]
[1, 2]
[1, 4]
Openjudge 百练第4109题的更多相关文章
- Poj OpenJudge 百练 1062 昂贵的聘礼
1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 T ...
- Poj OpenJudge 百练 1860 Currency Exchang
1.Link: http://poj.org/problem?id=1860 http://bailian.openjudge.cn/practice/1860 2.Content: Currency ...
- Poj OpenJudge 百练 1573 Robot Motion
1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...
- Poj OpenJudge 百练 2632 Crashing Robots
1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashin ...
- Poj OpenJudge 百练 2602 Superlong sums
1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlo ...
- Poj OpenJudge 百练 2389 Bull Math
1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Ma ...
- Poj OpenJudge 百练 Bailian 1008 Maya Calendar
1.Link: http://poj.org/problem?id=1008 http://bailian.openjudge.cn/practice/1008/ 2.content: Maya Ca ...
- [OpenJudge] 百练2754 八皇后
八皇后 Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. ...
- 百练6255-单词反转-2016正式B题
百练 / 2016计算机学科夏令营上机考试 已经结束 题目 排名 状态 统计 提问 B:单词翻转 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个 ...
随机推荐
- HTML资源(推荐)
W3C在线验证工具:http://validator.w3.org/ (X)HTML嵌套规则:http://www.cnblogs.com/PeunZhang/archive/2012/03/11/2 ...
- IE中对于stylesheet的个数限制
对于IE6-9: 1.样式规则最多只能有4095个,多于这个数目的会被忽略: 2.样式表(通过@import, <link> 或 <style>)最多可以有31个(总和),多于 ...
- ruby文档
http://www.ruby-doc.org/http://rubyonrails.org/https://www.ruby-lang.org/zh_cn/downloads/http://ruby ...
- mirantis fuel
使用mirantis公司的openstack部署工具--fuel,迅速部署体验openStack云管理软件; 1,使用virtualbox虚拟化工具(此工具在此使用主要是为了简化新建虚机的网络规划,f ...
- linux比较指令comm、diff、grep -Ff
Comm命令 如果想对两个有序的文件进行比较,可以使用comm命令. 语法:comm [- 123 ] file1 file2 说明:该命令是对两个已经排好序的文件进行比较.其中file1和file2 ...
- JS-Array数组内置对象
直接上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...
- WPF自定义下拉控件
可以搜索的下拉条 using System; using System.Collections; using System.Collections.Generic; using System.Coll ...
- SEO_Alexa排名
1.Alexa排名是评价某一网站访问量的一个指标,对SEO优化没有任何作用,这个数据是可以做假的,考虑Alexa排名纯粹是在浪费时间. 2.Alexa排名是针对已经在浏览器中安装了Alexa Tool ...
- UVALive 6525 Attacking rooks 二分匹配 经典题
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...
- android View各属性详解
一.有8个直接子类:AnalogClock, ImageView, KeyboardView, ProgressBar, SurfaceView, TextView, ViewGroup, ViewS ...