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
****************************************************************/

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

时间: 2024-12-25 07:28:09

Destroy Tunnels(矩阵水题)的相关文章

UVA 11551 - Experienced Endeavour(构造矩阵-水题)

题意:求一列序列的经过r次变化后的新序列,这些变化都是旧序列的某些已给位置的和产生新的项 思路:好水,直接构造01矩阵 //Accepted 45 ms C++ 4.8.2 1442 #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int mod= 1000; int num[55]; int res[55

Codeforces Round #113 (Div. 2)E---Tetrahedron(矩阵,水题)

You are given a tetrahedron. Let's mark its vertices with letters A, B, C and D correspondingly. An ant is standing in the vertex D of the tetrahedron. The ant is quite active and he wouldn't stay idle. At each moment of time he makes a step from one

5.1个人赛解题报告(区间dp,按位与或,图论等水题)

这次5.1打了一场个人赛,已经连赛了三周了,有点疲惫感觉,可能自己太水了,每次都有点小紧张. 这次只解出来三道题,然而有一道按位与按位或的水题不知道思路实在是做题太少,还有就是第一题区间DP,也消耗了不少的时间,但是没有成功的写出来,还是不够熟练啊. 下面写报告 A. System Administrator time limit per test 2 seconds memory limit per test 256 megabytes input standard input output

POJ2236 wireless network 【并查集水题】

前端开发whqet,csdn,王海庆,whqet,前端开发专家 今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁.结婚已经五年,但是也不可以偷懒,于是有了这个效果. 在线研究点这里,下载收藏点这里.程序猿and程序媛,大胆秀出你的爱吧. 利用html5 canvas实现动态的文字粒子效果,效果如下. OK,简单看看原理,首先我们需要在canvas里面实现描边文字,然后利用getImageData获得描边文字的像素矩阵,将粒子效果绑定在描边文章上. 整个效果如下. html文

HDU 4022 Bombing(基本算法-水题)

Bombing Problem Description It's a cruel war which killed millions of people and ruined series of cities. In order to stop it, let's bomb the opponent's base. It seems not to be a hard work in circumstances of street battles, however, you'll be encou

CF413B 水题

题意:有n个人,m个通讯设备,通讯k次.给出人可以通讯的矩阵.当一个人在某台设备通讯时,其他人可以接收,问k次通讯结束后每个人接收了多少次. 思路:开始没看清楚m的范围,如果简单暴力,通讯一次就把接收到的人加一次,这样要n*k的时间,一看就会超时.再一看m的取值范围是[1,10],所以我可以先不管是谁通讯,只要对某个设备通讯,就统计,最后一次把连接这个设备的人加上统计的值,但是通讯的那个人又不能接收,所以多加了他通讯的次数,只需在统计一下他通讯多少次就可以了. 代码: #include<stdi

2015南阳CCPC L - Huatuo&#39;s Medicine 水题

L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous doctor. He use identical bottles to carry the medicine. There are different types of medicine. Huatuo put medicines into the bottles and chain these b

sdut 2841 Bit Problem (水题)

题目 贴这个题是因为看题解有更简单的方法, 我做的时候是直接算的, 也很简单. 贴一下题解吧: 如果一个整数不等于 0,那么该整数的二进制表示中至少有一位是 1. 这个题结果可以直接输出 x - (x&(x-1)); 因为x-1 之后二进制下,就是最右边的1变成了0, 最右边的1的 右边所有的0变成了1, 不影响最左边. 我的代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4

sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)

n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you will step into a distinctive battleground which is full of sweet and happiness. If you want to win the battle, you must do warm-up according to my inst