HDU4324-Triangle LOVE-判断是否存在环(拓扑)
2015-11-20 22:22
246 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4324
题目意思:矩阵。1表示i-th喜欢j-th,问是否存在环状喜欢关系。
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=2005;
bool Map
;
int t,n,Case=1;
char str
;
struct node{
int in,out;
}s
;
int TopSort()
{
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(s[j].in==0){
s[j].in=-1;
for(int k=0;k<n;k++){
if(Map[j][k]){
s[k].in--;
}
}
}
}
}
for(int i=0;i<n;i++)
if(s[i].in>0) return 0;
return 1;
}
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(Map,false,sizeof(Map));
memset(s,0,sizeof(s));
for(int i=0;i<n;i++){
scanf("%s",str);
for(int j=0;j<n;j++){
if(str[j]=='1'){
s[i].out++;
s[j].in++;
Map[i][j]=true;
}
}
}
if(!TopSort()) printf("Case #%d: Yes\n",Case++);
else printf("Case #%d: No\n",Case++);
}
return 0;
}
题目意思:矩阵。1表示i-th喜欢j-th,问是否存在环状喜欢关系。
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=2005;
bool Map
;
int t,n,Case=1;
char str
;
struct node{
int in,out;
}s
;
int TopSort()
{
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(s[j].in==0){
s[j].in=-1;
for(int k=0;k<n;k++){
if(Map[j][k]){
s[k].in--;
}
}
}
}
}
for(int i=0;i<n;i++)
if(s[i].in>0) return 0;
return 1;
}
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(Map,false,sizeof(Map));
memset(s,0,sizeof(s));
for(int i=0;i<n;i++){
scanf("%s",str);
for(int j=0;j<n;j++){
if(str[j]=='1'){
s[i].out++;
s[j].in++;
Map[i][j]=true;
}
}
}
if(!TopSort()) printf("Case #%d: Yes\n",Case++);
else printf("Case #%d: No\n",Case++);
}
return 0;
}
相关文章推荐
- [笔记-架构探险]框架优化与功能扩展3.2.安全框架shiro、提供安全控制特性
- ubuntu java 环境变量设置
- 使用TCP协议的NAT穿透技术 (转)
- java深坑总结
- Web Service基础篇(一)之AXIS2
- linux shell编程 5 判断登陆用户,然后重启关闭系统
- 多线程——原子、非原子,自旋锁和互斥锁
- Android自动获得短信验证码
- LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)
- PHP正则表达式
- UVa-11292 - Dragon of Loowater:简单题
- Invert Binary Tree(easy)
- heheda练数据结构
- PHP语法(二):数据类型、运算符和函数
- Common JS、AMD、CMD和UMD的区别
- Invert Binary Tree(easy)
- 2.1 状态空间表示
- python实现爬虫统计学校BBS男女比例(二)多线程爬虫
- Servlet中的GET和POST之间的区别
- ISE使用IP核生成的SRAM,读写需要时钟的原因