博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3997 Stock Chase
阅读量:5222 次
发布时间:2019-06-14

本文共 3219 字,大约阅读时间需要 10 分钟。

Stock Chase
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 455   Accepted: 131

Description

I have to admit, the solution I proposed last year for solving the bank cash crisis didn’t solve the whole economic crisis. As it turns out, companies don’t have that much cash in the first place. They have assets which are primarily shares in other companies. It is common, and acceptable, for one company to own shares in another. What complicates the issue is for two companies to own shares in each other at the same time. If you think of it for a moment, this means that each company now (indirectly) controls its own shares.
New market regulation is being implemented: No company can control shares in itself, whether directly or indirectly. The Stock Market Authority is looking for a computerized solution that will help it detect any buying activity that will result in a company controlling its own shares. It is obvious why they need a program to do so, just imagine the situation where company A buying shares in B, B buying in C, and then C buying in A. While the first two purchases are acceptable. The third purchase should be rejected since it will lead to the three companies controlling shares in themselves. The program will be given all purchasing transactions in chronological order. The program should reject any transaction that could lead to one company controlling its own shares. All other transactions are accepted.

Input

Your program will be tested on one or more test cases. Each test case is specified on T + 1 lines. The first line specifies two positive numbers: (0 < N ≤ 234) is the number of companies and (0 < T ≤ 100, 000) is the number of transactions. T lines follow, each describing a buying transaction. Each transaction is specified using two numbers A and B where (0 < A,B ≤ N) indicating that company A wants to buy shares in company B.
The last line of the input file has two zeros.

Output

For each test case, print the following line:
k. R
Where k is the test case number (starting at one,) R is the number of transactions that should be rejected.

Sample Input

3 61 21 33 12 11 22 30 0

Sample Output

1. 2

Source

 
题目大意:有N个公司,A公司可以买B公司的股票, B公司可以买C公司的股票,如此A公司会间接持有C公司的股票,这种操作非法,所以不能传递,按照时间顺序给出T次购买请求,问有多少次非法的购买请求会被拒绝。
 
解题思路:运用dp的思想压缩下路径。
 
 
#include 
#include
using namespace std;const int maxn=300;bool dp[maxn][maxn];int n,m,casen=0;void initial(){ for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) dp[i][j]=false; for(int i=0;i<=n;i++) dp[i][i]=true;}void computing(){ int ans=0,u,v; while(m--){ scanf("%d%d",&u,&v); if(dp[v][u]){ ans++; }else if(!dp[u][v]){ dp[u][v]=true; for(int i=1;i<=n;i++){ if(!dp[i][u]) continue; dp[i][v]=true; for(int j=1;j<=n;j++){ if(!dp[v][j]) continue; dp[i][j]=true; dp[u][j]=true; } } } } casen++; printf("%d. %d\n",casen,ans);}int main(){ while(scanf("%d%d",&n,&m)!=EOF && (m||n)){ initial(); computing(); } return 0;}
 
 

转载于:https://www.cnblogs.com/toyking/p/3797378.html

你可能感兴趣的文章
.NET方向高级开发人员面试时应该事先考虑的问题
查看>>
台达PLC modbus 不支持04功能码
查看>>
python学习笔记--装饰器
查看>>
发布一个JavaScript工具类库jutil,欢迎使用,欢迎补充,欢迎挑错!
查看>>
discuz 常用脚本格式化数据
查看>>
MS CRM 2011 创建基于Fetch的报表 -- 进阶版
查看>>
zabbix 监控zookeeper
查看>>
trace与代码跟踪服务
查看>>
Fire!
查看>>
洛谷P2777
查看>>
Ajax
查看>>
PHPStorm2017设置字体与设置浏览器访问
查看>>
android开发学习笔记:圆角的Button
查看>>
Activity简介
查看>>
jqGrid树
查看>>
循环-12. 打印九九口诀表(15)
查看>>
oracle树状索引详解(图摘取《收获不止oracle》)
查看>>
Android Studio 设置代码提示和代码自动补全快捷键--Eclipse 风格 - 转
查看>>
ORACLE基本操作备忘
查看>>
Maven学习:项目之间的关系
查看>>