標籤:des style blog http color io os ar java
Go Deeper
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2198 Accepted Submission(s): 722
Problem DescriptionHere is a procedure‘s pseudocode:
go(int dep, int n, int m)
begin
output the value of dep.
if dep < m and x[a[dep]] + x[b[dep]] != c[dep] then go(dep + 1, n, m)
end
In this code n is an integer. a, b, c and x are 4 arrays of integers. The index of array always starts from 0. Array a and b consist of non-negative integers smaller than n. Array x consists of only 0 and 1. Array c consists of only 0, 1 and 2. The lengths of array a, b and c are m while the length of array x is n. Given the elements of array a, b, and c, when we call the procedure go(0, n, m) what is the maximal possible value the procedure may output?
InputThere are multiple test cases. The first line of input is an integer T (0 < T ≤ 100), indicating the number of test cases. Then T test cases follow. Each case starts with a line of 2 integers n and m (0 < n ≤ 200, 0 < m ≤ 10000). Then m lines of 3 integers follow. The i-th(1 ≤ i ≤ m) line of them are ai-1 ,bi-1 and ci-1 (0 ≤ ai-1, bi-1 < n, 0 ≤ ci-1 ≤ 2).
OutputFor each test case, output the result in a single line.
Sample Input3 2 10 1 0 2 10 0 0 2 20 1 01 1 2
Sample Output112
AuthorCAO, Peng
Source2010 Asia Chengdu Regional Contest
題目的意思是求出合格能去到的最大深度 。
然後 x 只有 0 , 1 兩種 。
明顯就是一個 two - sat .
然後二分一個深度 , 重新構圖 , 看下所有限制能否都符合 。
卡了一下二分 。 。
構圖我是反向的 , 即 u - v 表示這兩個條件不能共存
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <map>using namespace std;typedef long long LL;const int N = 20010;const int M = 800010;int eh[N] , et[M], nxt[M] , tot ;bool mark[N] ;int n , s , a[N] , b[N] ,c[N];int st[N] , top ;void addedge(int u , int v ){ et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot++; et[tot] = u , nxt[tot] = eh[v] , eh[v] = tot++;}void init(){ memset (mark , false , sizeof mark ); tot = 0 ; memset ( eh , -1 , sizeof eh );}// ----------------bool dfs( int u ){ if( mark[u] ) return true ; if( mark[u^1] ) return false ; mark[u] = true; st[top++] = u ; for( int i = eh[u] ; ~i ; i = nxt[i] ){ int v = et[i] ; if( !dfs( v^1 ) ) return false; } return true ;}bool solve( int m ){ init(); for( int i = 0 ; i < m ; ++i ){ if( c[i] == 0 ){ addedge( 2*a[i] , 2*b[i] ); } else if( c[i] == 1 ){ addedge( (2*a[i])^1 , 2*b[i] ); addedge( (2*b[i])^1 , 2*a[i] ); } else { addedge( (2*a[i])^1 , (2*b[i])^1 ); } } for( int i = 0 ; i < 2 * n ; i+=2 ){ if( !mark[i] && !mark[i+1] ){ top = 0 ; if( !dfs(i) ){ while( top > 0 ) mark[ st[--top] ] = false ; if( !dfs(i+1) ) return false ; } } } return true;}void run(){ int m ; scanf("%d%d",&n,&m); for( int i = 0 ; i < m ;++i ) scanf("%d%d%d",&a[i],&b[i],&c[i]); int ans = 1 , l = 1 ,r = m ; while( l <= r ){ int mid = ( l + r ) / 2;// cout << mid <<‘ ‘<<endl ; if( solve(mid) ) l = mid + 1 , ans = mid ; else r = mid - 1; } printf("%d\n",ans);}int main(){ #ifdef LOCAL freopen("in","r",stdin); #endif int _ ; scanf("%d",&_); while( _ -- )run(); return 0;}
HDU 3715 Go Deeper