CSU1612: Destroy Tunnels

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

Source

题意:根据题目要求的公式,能不能让矩阵都变成非0

由于数学并非我的强项,其实这道题更多是猜的。。

#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;
}
时间: 2024-09-30 14:45:17

CSU1612: Destroy Tunnels的相关文章

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

CSU 1612: Destroy Tunnels 强连通分量 Kosaraju算法

链接 :  http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1612 题意: 给一个矩阵A 大小N*N, B = A^1 + A^2 + A^3 + .... A^n , B中是否存在非0项. 题目可以转化为 N个点 编号为1-n, 对于任意点v,经过一些任意的步长到达u (u为所有点集的任意一个).离散数学里有图的矩阵相关知识 A^k代表了矩阵中从i到j的步长为k的方法数. 题目就是求整个图是否强连通. 学习了 Kosaraju算法 可以轻

Delphi的对象注销方法Destroy和free

当您使用完对象后,您应该及时撤销它,以便把这个对象占用的内存释放出来.您可以通过调用一个注销方法来撤销您的对象,它会释放分配给这个对象的内存. Delphi的注销方法有两个:Destroy和Free.Delphi建议使用Free,因为它比Destroy更为安全,同时调用Free会生成效率更高的代码. 您可以用下列的语句释放用完的Employee对象: Employee.Free; 和Create方法一样,Free方法也是TEmployee从TObject中继承过来的.把您的注销放在try…fin

Command to destroy cluster

Command to destroy cluster: Get-Cluster Cluster1 | Remove-Cluster -Force-CleanupAD https://technet.microsoft.com/en-us/library/ee461005.aspx

Caves and Tunnels

Caves and Tunnels Time limit: 3.0 secondMemory limit: 64 MB After landing on Mars surface, scientists found a strange system of caves connected by tunnels. So they began to research it using remote controlled robots. It was found out that there exist

Configure Open vSwitch Tunnels

转自: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux_OpenStack_Platform/4/html/Installation_and_Configuration_Guide/Configuring_Open_vSwitch_tunnels.html 13.4.4. Configure Open vSwitch Tunnels Tunneling with Open vSwitch allows

HDU 4856 Tunnels(bfs+状压dp)

题目大意:给你一个N*N的图让你到达所有的"."点,"#"不能通过,有m组每组有一个入口,一个出口,入口可以传送到出口,不知道经过m组的先后顺序,让你求出走过所有的"."的最小时间. 思路:先bfs出来所有的m之间的最短距离,然后dp[j][i] 表示,在j状态下开始第i步的最小路程,枚举找到一个最小的dp[1<<m - 1][i],就是最短距离,否则输出"-1". Tunnels Time Limit: 3000

HDU 4856 Tunnels

Tunnels Time Limit: 1500ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 485664-bit integer IO format: %I64d      Java class name: Main Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In his eyes, t

angularjs中的$destroy和$timeout

module.controller("TestController", function($scope, $timeout) { var onTimeout = function() { $scope.value += 1; timer = $timeout(onTimeout, 1000); }; var timer = $timeout(onTimeout, 1000); $scope.value = 0; $scope.$on("$destroy", func