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 描述 输入一个 ...
随机推荐
- web api 2 学习笔记 (Odata ODataQueryOptions 使用)
[ODataRoutePrefix("products")] public class ProductController : BaseController { [ODataRou ...
- JVM-ClassLoader(转)
在加载阶段主要用到的是方法区: 方法区是可供各条线程共享的运行时内存区域.存储了每一个类的结构信息,例如运行时常量池(Runtime Constant Pool).字段和方法数据.构造函数和普通方法的 ...
- hdu3534,个人认为很经典的树形dp
题目大意为,求一个树的直径(最长路),以及直径的数量 朴素的dp只能找出某点开始的最长路径,但这个最长路径却不一定是树的直径,本弱先开始就想简单了,一直wa 直到我看了某位大牛的题解... 按照那位大 ...
- HDU-4857(拓扑排序)
Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前. ...
- red-hat6.5 yum 源配置,cloud-init 安装 This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register
This system is not registered to Red Hat Subscription Management. You can use subscription-manager t ...
- ORACLE REFERENCES FRO TEST
[JSU]LJDragon's Oracle course notes In the first semester, junior year Oracle考前复习 试题结构分析: 1.选择题2x10, ...
- 定义的返回按钮 Push到下一个页面后 手势返回无效解决办法
转自:http://zhangmingwei.iteye.com/blog/2080457 从iOS7的Beta版开始,就着手做兼容工作,到Beta4的時候,应用已经基本兼容,只是偶然发现,iOS样式 ...
- Java开发者易犯错误Top10
本文总结了Java开发者经常会犯的前十种错误列表. Top1. 数组转换为数组列表 将数组转换为数组列表,开发者经常会这样做: List<String> list = Arrays.asL ...
- Java基础知识强化67:基本类型包装类之Integer直接赋值的面试题
1. 面试题: Integer i = 1: i += 1: 做了哪些事情? (1)其中Integer i =1:做了自动装箱( 使用valueOf()方法,int ---> Integer ...
- CentOS6.7 常用操作命令
centos 安装py环境 1.安装wget工具: yum install wget 2.安装Python-2.7.8: wget --no-check-certificate https://www ...