HihoCoder(79 -80weeks)
来源:互联网 发布:台湾视频软件 编辑:程序博客网 时间:2024/04/28 01:22
第79周
题目描述:
就是一个超级计算机有N个单元,每个单元有两种状态,开和关。然而对某些单元,当他们在相同的状态下时,会对机器造成不稳定。现在告诉你会造成不稳定的情况,问该计算是否存在一个稳定的状态,是的话输出“YES”,否则输出“NO”。
解答:
题意比较清晰,是个纯裸的二分图,对于i,j不能同时为状态st进行处理,然后可以得到一条语句就是i为1-st,或者j为1-st,然后就可以解决了,看这个二分图里面是否存在解。
代码如下:
#include <iostream>#include <vector>#include <cstring>using namespace std;const int maxn = 10005;struct TwoSat{int n, S[maxn * 2], c;vector<int> G[maxn * 2];bool mark[maxn * 2];bool dfs(int x){if(mark[x ^ 1]) return false;if(mark[x]) return true;mark[x] = true; S[c ++] = x;int len = G[x].size();for(int i = 0; i < len; i ++)if(!dfs(G[x][i])) return false;return true;}void init(int n){this->n = n;for(int i = 0; i < n * 2; i ++) G[i].clear();memset(mark, false, sizeof(mark));}void addEdge(int x, int xval, int y, int yval){ //染色加边,x为真,xval = 1....x = x * 2 + xval; y = y * 2 + yval;G[x^1].push_back(y); G[y^1].push_back(x);}bool solve(){for(int i = 0; i < n * 2; i += 2){if(!mark[i] && !mark[i + 1]){c = 0;if(!dfs(i)){while(c > 0) mark[S[--c]] = false;if(!dfs(i + 1)) return false;}}}return true;}};int T, N, M;void solve(){TwoSat solver;solver.init(N);int a, b, st;for(int i = 0; i < M; i ++){cin>>a>>b>>st;a -= 1, b -= 1;solver.addEdge(a, 1 - st, b, 1 - st);}cout<< (solver.solve() ? "Yes" : "No") <<endl;}int main(){while(cin>>T){while(T --){cin>>N>>M;solve();}}}
第80周
题目描述:
就是给你一个序列,里面有三种标记分别为R,Y,B的物体,一个接一个的放入木箱中,然后给你一个x,y,z。当三种物体个数之差的绝对值刚好是x,y,z时,就将这些物体都拿出木箱中,要求出木箱中含有物体最多时的个数。
解答:
简单的模拟题,没啥说的,模拟一下就好了。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>using namespace std;int x, y, z;string s;bool isOK(int *num){for(int i = 0; i < 3; i ++)num[i] < 0 ? num[i] *= -1: 0;sort(num, num + 3);if(num[0] == x && num[1] == y && num[2] == z)return true;return false;}void solve(){int ans = 0;int num[3] = {0};int len = s.length();if(x > y) swap(x, y);if(y > z) swap(y, z);if(x > y) swap(x, y);for(int i = 0, cnt = 0; i < len; i ++){if(s[i] == 'R') num[0] ++, num[2] ++;else if(s[i] == 'B') num[0] -- , num[1] ++;else num[2] --, num[1] --;cnt ++;ans = ans < cnt ? cnt : ans;int nnn[3];nnn[0] = num[0], nnn[1] = num[1], nnn[2] = num[2];if(isOK(nnn)) {cnt = 0;num[0] = num[1] = num[2] = 0;}}cout<<ans<<endl;}int main(){while(cin>>x>>y>>z){cin>>s;solve();}}
0 0
- HihoCoder(79 -80weeks)
- 7weeks
- hihoCoder week 80
- hihocoder:
- hihoCoder
- Hihocoder
- hihocoder
- hihocoder
- hihocoder
- hihocoder
- hihocoder
- HihoCoder
- HihoCoder
- HihoCoder
- HihoCoder
- HihoCoder
- HihoCoder
- hihoCoder
- cognos RS 进度条
- java提高篇(十)-----强制类型转换
- 随机梯度下降与梯度下降
- 解决虚拟机中ubuntu无法识别U盘
- Android 主流网络图片加载缓存库框架Universal-ImageLoader的使用
- HihoCoder(79 -80weeks)
- ssh到openwrt时遇到的问题
- poj1979——red and black
- C++:面向对象编程(封装、继承、多态等)
- 【转】积性函数前缀和的求法——By Tangjz
- oracle 存储过程 基础
- 深入浅出设计模式(五):7.适配器模式
- 你应该知道的Unicode
- 基于UDP的单线程网络点对点数据包收发测试程序