ZOJ 2856 Happy Life
Do you know Utopia? It's a perfect world in which everyone leads a happy life.
A fairy wants to make a naive Utopia City. She studies the factors that have an impact on people's happiness and thinks that a person is happy if and only if all other persons' total "influence factor" on him (or her) is nonnegative. Each person has an influence factor on another person, which may be a positive integer or negative integer or 0. The influence factor is always symmetric, that is, if person A has an influence factor f on person B, it means that person B also has an "influence factor" f on A. So we can say the influence factor between person A and B is f without confusion. A person's influence factor on himself (or herself) is always 0. So let f(i, j) be the influence factor between person i and person j and person i's happiness(i) is well defined as follows:
Obviously there may be some persons who are not happy. Though the fairy cannot change any influence factor, she can give every person a property p(i) which is always +1 or -1. Under the fairy's magical definition, a person i's happiness(i)' is redefined as follow:
Person i feels happy if the value of happiness(i)' is nonnegative. But the fairy wonders whether she can give everyone a property to make all of them happy so that she can build her ideal naive Utopia successfully.
Since you're an ace programmer, the fairy asks you to help her to fulfill her dream. Can you help her?
Input
The input contains multiple test cases!
Each test case starts with an integer N (2 <= N <= 200), the number of persons in the city. After that there're N lines of integers and each line consists of N integers. The j-th integer of the i-th line of the matrix indicates the influence factor f(i,j) (-1000 < f(i,j) < 1000).
Proceed to the End Of File (EOF).
Output
For each test case, if the fairy fails, output a single line with "No" (without the quotations), otherwise output "Yes" (without the quotations) in the first line, followed by N lines, each line contains exactly a "+" (without the quotations) or a "-"(without the quotations) to indicate that the fairy should give the i-th person property +1 or -1 to fulfill her dream.
Sample Input
3
0 1 3
1 0 -1
3 -1 0
2
0 -10
-10 0
Sample Output
Yes
-
+
-
Yes
+
-
暴力,为什么不会出现No的情况呢?
#include <stdio.h>
#define MAXN 220 int n;
int g[MAXN][MAXN];
int ans[MAXN]; int main(){
int i,j;
while( scanf("%d",&n)!=EOF ){
for(i=1; i<=n; i++){
for(j=1; j<=n; j++){
scanf("%d" ,&g[i][j]);
}
ans[i]=1;
}
int index=1;
while(1){
if(index==n+1){
break;
}
int sum=0;
for(i=1; i<=n; i++){
sum+=ans[i]*g[index][i];
}
if(sum*ans[index]<0){
ans[index]*=-1;
index=1;
}
else index++;
}
puts("Yes");
for(int i=1; i<=n; i++){
if(ans[i]<0)
puts("-");
else
puts("+");
}
}
return 0;
}
ZOJ 2856 Happy Life的更多相关文章
- ZOJ 2856 Happy Life 暴力求解
因为是Special Judge 的题目,只要输出正确答案即可,不唯一 暴力力求解, 只要每次改变 happiness 值为负的人的符号即可. 如果计算出当前人的 happiness 值为负,那么将其 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...
- ZOJ Problem Set - 1006 Do the Untwist
今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...
- ZOJ Problem Set - 1001 A + B Problem
ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...
- zoj 1788 Quad Trees
zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...
随机推荐
- uva639 回溯!
#include<iostream> using namespace std; int n,Max,C[4][4]; char board[5][5]; bool vis[16]; boo ...
- MSSQL Server中partition by与group by的区别
在使用over等开窗函数时,over里头的分组及排序的执行晚于“where,group by,order by(但此排序顺序优先级是最高的)”的执行. ①group by 列名 合并(列值相同的并作一 ...
- 构建基于asp.net core 的docker应用并发布
发布Docker镜像的方法有很多种,asp.net core的发布需要在windows系统中 开门见山,首先保证已经在Centos上安装好了Docker.创建一个asp.net core的webapi ...
- micropython esp8266 烧录
MicroPython 介绍 MicroPython 是一个 Python 3 语言的精简.高效实现,其包括 Python 标准库的一小部分,并经过优化,可以运行在微控制器和受限环境中运行. Micr ...
- C#中数组作为参数传递的问题
原则:尽可能控制对数据的修改,如果可以预测某个数据不会或不应该被改变,就要对其控制,而不要期望使用这个数据的调用者不会改变其值. 如果参数在使用过程中被意外修改,将会带来不可预知的结果,而且这种错误很 ...
- 「AH2017/HNOI2017」礼物
题目链接 戳我 \(Solution\) 应为我们可以将任意一个数列加上一个非负整数,即可以变为将一个数列加上一个整数(可以为负),我们将这个整数设为\(z\).所以要求的式子的变为: \[\sum_ ...
- 16、xtrabackup 增量备份及恢复
备份命令如下 备份命令如下 全量备份 # innobackupex -p123123 /backup # ls /backup 2017-04-08_13-36-11 增量备份或差量备份 # inn ...
- 【bzoj4887】:[Tjoi2017]可乐 矩阵乘法,快速幂
[bzoj4887]:[Tjoi2017]可乐 题目大意:一张无相连通图(n<=30),从1号点开始走,每秒可以走到相邻的点也可以自爆,求第t秒(t<=1e6)后所有的方案数是多少对201 ...
- jquery事件三 -- load(), ready(), resize()以及bind()事件
例子1 ready() DOM加载完毕 load() 元素加载完毕 resize() 浏览器窗口的大小发生变化 <!DOCTYPE html> <html lang="en ...
- Rstudio常用快捷键 “原版+中文” 整理