Go Deeper
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 1
Problem Description
Here 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?
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?
Input
There 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 a
i-1 ,b
i-1 and c
i-1 (0 ≤ a
i-1, b
i-1 < n, 0 ≤ c
i-1 ≤ 2).
Output
For each test case, output the result in a single line.
Sample Input
3 2 1 0 1 0 2 1 0 0 0 2 2 0 1 0 1 1 2
Sample Output
1 1 2
Author
CAO, Peng
Source
2010 Asia Chengdu Regional Contest
题意还算明显,二分长度,2-SAT判定,结果二分最后的结果不知道怎么找了,果断2B了。。。
不过还是A了,用一个变量随时保存最新更新成功的长度就好了
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <stdio.h> 2 #include <string.h> 3 #define maxn 444 4 #define inf 1e9 5 typedef struct edge{ 6 int v; 7 edge *next; 8 }edge; 9 edge map[maxn],e[100000]; 10 int n,m,k,cnt; 11 int a[10005],b[10005],c[10005]; 12 void addedge(int u,int v){ 13 edge *p=e+cnt++; 14 p->v=v; p->next=map[u].next; map[u].next=p; 15 } 16 void init(){ 17 cnt=0; 18 memset(map,0,sizeof(map)); 19 scanf("%d%d",&n,&m); 20 for(int i=0;i<m;i++){ 21 scanf("%d%d%d",&a[i],&b[i],&c[i]); 22 } 23 } 24 int low[maxn],dfn[maxn],s[maxn],belong[maxn],top,deep,num; 25 bool ins[maxn]; 26 void dfs(int u){ 27 low[u]=dfn[u]=deep++; 28 ins[u]=true; s[top++]=u; 29 for(edge *e=map[u].next;e;e=e->next){ 30 int v=e->v; 31 if(!dfn[v]){ 32 dfs(v); 33 low[u]=low[u]<low[v]?low[u]:low[v]; 34 }else if(ins[v]) low[u]=dfn[v]<low[u]?dfn[v]:low[u]; 35 } 36 if(low[u]==dfn[u]){ 37 int v; 38 do{ 39 v=s[--top]; 40 belong[v]=num; 41 ins[v]=false; 42 }while(v!=u); num++; 43 } 44 } 45 bool check(int len){ 46 memset(map,0,sizeof(map)); 47 cnt=0; 48 for(int i=0;i<len;i++){ 49 if(c[i]==0){ 50 addedge(a[i],b[i]+n); 51 addedge(b[i],a[i]+n); 52 }else if(c[i]==1){ 53 addedge(a[i],b[i]); 54 addedge(b[i],a[i]); 55 addedge(a[i]+n,b[i]+n); 56 addedge(b[i]+n,a[i]+n); 57 }else if(c[i]==2){ 58 addedge(a[i]+n,b[i]); 59 addedge(b[i]+n,a[i]); 60 } 61 }deep=1;top=0;num=1; 62 memset(ins,0,sizeof(ins)); 63 memset(low,0,sizeof(low)); 64 memset(dfn,0,sizeof(dfn)); 65 for(int i=0;i<2*n;i++)if(!dfn[i]) dfs(i); 66 for(int i=0;i<n;i++)if(belong[i]==belong[i+n]) return false; 67 return true; 68 } 69 70 int main() 71 { 72 freopen("in.txt","r",stdin); 73 int t; 74 scanf("%d",&t); 75 while(t--){ 76 init(); 77 int l=0,r=m,mid,ans=l; 78 while(l<=r){ 79 mid=(l+r)>>1; 80 if(check(mid)){ans=mid; l=mid+1;} 81 else r=mid-1; 82 } 83 printf("%d\n",ans); 84 } 85 return 0; 86 }