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 描述 输入一个 ...
随机推荐
- Effective Go -> Interface
1.接口实现及类型转换 type Sequence []int // Methods required by sort.Interface. func (s Sequence) Len() int { ...
- angular2 学习笔记 ( ngModule 模块 )
2016-08-25, 当前版本是 RC 5. 参考 : https://angular.cn/docs/ts/latest/guide/ngmodule.html 提醒 : 这系列笔记的 " ...
- DACL, NULL or not NULL
上周 hBifTs在折腾他的文件映射封装类的时候,碰到了不能在 ASP.NET 中直接打开由桌面程序创建的内核对象的问题. 内存映射文件与用户权限 他当时是的方法是修改 ASP.NET 配置文件,让 ...
- 在ubuntu上编译chrome
在ubuntu上编译chrome 在ubuntu上编译chrome 红心地瓜 1.获取代码 1)下载tarball,http://chromium-browser-source.commondatas ...
- 【转】文件同步软件FreeFileSync
原文网址:http://imcn.me/html/y2012/9855.html FreeFileSync 是一款开源的文件夹比较和同步工具,可用于 Win 和 Lin 平台,最近发布了 5.0 版本 ...
- 【细说Java】揭开Java的main方法神秘的面纱
大家都知道,main方法是Java应用程序的入口,其定义格式为: public static void main(String[] args) 可是为什么要这么定义呢?不这样定义可以么?main方法可 ...
- LINQ 内链接 左链接 右链接
原文地址:http://blog.sina.com.cn/s/blog_46e9573c01014fx2.html 1.左连接: var LeftJoin = from emp in ListOfEm ...
- USACO6.4-Electric Fences:计算几何
Electric Fences Kolstad & Schrijvers Farmer John has decided to construct electric fences. He ha ...
- css浮动+应用(瀑布流效果的实现)
首先是index.html文件: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...
- (转)Linux下Apache 限速模块安装笔记
参考文章:http://www.pcmag.com.cn/solution/net/story/200704/51003104.shtml 限线程:http://dominia.org/djao/li ...