Destroy Tunnels(矩阵水题)

 Destroy Tunnels

Time Limit: 2 Sec   Memory Limit: 128 MB
Submit: 39   Solved: 22
[ Submit][ Status][ Web Board]

Description

Zuosige always has bad luck. Recently, he is in hospital because of pneumonia. While he is taking his injection, he feels extremely bored. However, clever Zuosige comes up with a new game.

Zuosige is playing a computer game called Call of Pneumonia. In the game, Zuosige is pursued by germs which may cause pneumonia. Zuosige is feared, so he wants to destroy the country. The country consists of N cities and for each city, there exits exactly one directed tunnel from it to every other city.

To destroy bridges, Zuosige gets a magic matrix An*n (Ai,j>=0). Then let  , if Bi,j>0, the bridge from city i to city j will be destroyed.

Now Zuosige wants to know whether he can destroy all tunnels.

Input

The first line contains one integer T, indicating the number of test cases.
In one test case, there are several lines. 
In the first line, there is one integer N (1<=n<=1000), indicating the number of cities.
In the following N lines, each line has N integers. These N lines describes matrix A. The j-th integer in i-th line is Ai,j.

Output

For each test case, output “not exists” if all tunnels can be destroyed or “exists” otherwise.

Sample Input

2
3
0 1 3
2 0 1
1 1 0
3
0 1 0
0 0 0
0 0 0

Sample Output

not exists
exists

HINT

 

 

比赛的时候,居然被卡,我的失误啊,,,都怪自己想太多。

 

其实只要某一行或者列全为零,那么必定不会转化为全非0的。仔细一想,行*列。

 

 

#include<stdio.h>
#define MM 1005
int mat[MM][MM];
 
int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int cnt,flag=0;
        for(int i=0;i<n;i++)
        {
            cnt=0;
            for(int j=0;j<n;j++)
            {
                scanf("%d",&mat[i][j]);
                if(mat[i][j]==0) cnt++;
            }
            if(cnt==n) flag=1;
        }
        if(flag){printf("exists\n");continue;}
        for(int i=0;i<n;i++)
        {
            cnt=0;
            for(int j=0;j<n;j++)
            {
                if(mat[j][i]==0) cnt++;
            }
            if(cnt==n) flag=1;
        }
        if(flag) printf("exists\n");
        else printf("not exists\n");
    }
    return 0;
}
 
 
 
 
 
/**************************************************************
    Problem: 1612
    User: aking2015
    Language: C++
    Result: Accepted
    Time:176 ms
    Memory:4908 kb
****************************************************************/

 

 

 

比赛的时候是这样过的

 

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define N 1000005
#define mod 19999997
const int INF = 0x3f3f3f3f;
#define exp 1e-8
 
bitset<1005> a[1005];
 
int main()
{
    int t,n,i,j,k,flag;
    cin>>t;
    w(t--)
    {
        cin>>n;
        up(i,1,n)
        {
            up(j,1,n)
            {
                cin>>k;
                a[i][j] = (k>0?1:0);
            }
        }
        up(i,1,n)
        {
            up(j,1,n)
            if(a[j][i]) a[j]|=a[i];
        }
        flag = 0;
        up(i,1,n)
        {
            up(j,1,n)
            {
                if(i==j)continue;
                if(!a[i][j])
                {
                    flag=1;
                    break;
                }
            }
            if(flag) break;
        }
        if(flag) printf("exists\n");
        else
            printf("not exists\n");
    }
 
    return 0;
}
 
/**************************************************************
    Problem: 1612
    User: aking2015
    Language: C++
    Result: Accepted
    Time:916 ms
    Memory:1608 kb
****************************************************************/


是时候该开个专题攻攻组合数学了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值