https://icpc.njust.edu.cn/Problem/Local/1928/ (2-sat)
题意:给你n关,每关两个数,然后你在每关必须选一个数,然后如果你以前选过数a,现在就不能选2n-1-a这个数,n数据量1000,问你最多可以走多少关
题解:2个数必须选一个,典型的2-sat问题,加上问你最多多少关,二分判定可行性,非常典型非常好的题目,然后n是1000,二分+O(n^2)的建图,O(n^2logn)可过
O(n^2logn)版本,实际速度飞快,仅为O(nlogn)复杂度的2倍时间
#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <string> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <sstream> #include <cstdlib> #include <iostream> #include <algorithm> #pragma comment(linker,"/STACK:102400000,102400000") using namespace std; #define MAX 2005 #define MAXN 1000005 #define maxnode 10 #define sigma_size 2 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lrt rt<<1 #define rrt rt<<1|1 #define middle int m=(r+l)>>1 #define LL long long #define ull unsigned long long #define mem(x,v) memset(x,v,sizeof(x)) #define lowbit(x) (x&-x) #define pii pair<int,int> #define bits(a) __builtin_popcount(a) #define mk make_pair #define limit 10000 //const int prime = 999983; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-9; const LL mod = 1e9+7; const ull mxx = 1333331; /*****************************************************/ inline void RI(int &x){ char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; } /*****************************************************/ int a[1005],b[1005]; struct Edge{ int u,v,next; }edge[MAX*MAX]; int dfn[MAX],low[MAX],belong[MAX],sstack[MAX],instack[MAX]; int head[MAX],tot,Index,top,cnt;// tot是建图//cnt是强联通分量个数 int n; void init(){ mem(head,-1); mem(instack,0); mem(dfn,0); tot=0; top=0; cnt=0; Index=0; } void add_edge(int a,int b){ edge[tot]=(Edge){a,b,head[a]}; head[a]=tot++; } void tarjan(int u){//判断可行只需要一个tarjan即可 dfn[u]=low[u]=++Index; sstack[++top]=u; instack[u]=1; for(int i=head[u]; i!=-1; i=edge[i].next){ int v=edge[i].v; if(!dfn[v]){ tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]){ ++cnt; while(1){ int k=sstack[top--]; instack[k]=0; belong[k]=cnt; if(k==u) break; } } } bool solve(int x){ init(); for(int i=1;i<=x;i++){ for(int j=i+1;j<=x;j++){ if(a[i]+a[j]==2*n-1){ add_edge(i,j+x); add_edge(j,i+x); } if(a[i]+b[j]==2*n-1){ add_edge(i,j); add_edge(j+x,i+x); } if(b[i]+a[j]==2*n-1){ add_edge(i+x,j+x); add_edge(j,i); } if(b[i]+b[j]==2*n-1){ add_edge(i+x,j); add_edge(j+x,i); } } } for(int i=1;i<=2*x;i++){ if(!dfn[i]) tarjan(i); } int flag=0; for(int i=1;i<=x;i++){ if(belong[i]==belong[i+x]) flag=1; } if(flag) return false; return true; } int main(){ //freopen("in.txt","r",stdin); int t; cin>>t; while(t--){ cin>>n; for(int i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]); int l=1,r=n; while(l<=r){ int mid=(l+r)/2; if(solve(mid)) l=mid+1; else r=mid-1; } cout<<r<<endl; } return 0; }
还有就是O(n)建图的版本,其实这题完全n可以出到10W,然后O(n)版本的优势就体现了,也是二分判定可行性,总复杂度O(nlogn)
如何O(n)建图呢,就是用值来建图,考虑如果选了2n-1-a[i],则不能选a[i],就必选b[i],所以一条边是(2n-1-a[i],b[i]),另外条边是(2n-1-b[i],a[i])
为什么呢,这样也是2sat的意思把,然后只有前x关(x是二分的值)的a和b会被建图,所以,只用到了前x关的点的值,然后一样tarjan求强连通分量,如果a和2n-1-a在一个连通分量里,就是不成立咯。可以自己画图试试,因为如果选了a,则必选b,选了b则必选2n-1-a,选了2n-1-a则必选c,选了c则必选a,这样必定会矛盾,所以其实和上面的判定是一样的,只是这个用了值来建图,复杂度优化到了O(nlogn),更优,需要掌握
#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <string> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <sstream> #include <cstdlib> #include <iostream> #include <algorithm> #pragma comment(linker,"/STACK:102400000,102400000") using namespace std; #define MAX 2005 #define MAXN 1000005 #define maxnode 10 #define sigma_size 2 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lrt rt<<1 #define rrt rt<<1|1 #define middle int m=(r+l)>>1 #define LL long long #define ull unsigned long long #define mem(x,v) memset(x,v,sizeof(x)) #define lowbit(x) (x&-x) #define pii pair<int,int> #define bits(a) __builtin_popcount(a) #define mk make_pair #define limit 10000 //const int prime = 999983; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-9; const LL mod = 1e9+7; const ull mxx = 1333331; /*****************************************************/ inline void RI(int &x){ char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; } /*****************************************************/ int a[1005],b[1005]; struct Edge{ int u,v,next; }edge[MAX*MAX]; int dfn[MAX],low[MAX],belong[MAX],sstack[MAX],instack[MAX]; int head[MAX],tot,Index,top,cnt;// tot是建图//cnt是强联通分量个数 int n; void init(){ mem(head,-1); mem(instack,0); mem(dfn,0); tot=0; top=0; cnt=0; Index=0; } void add_edge(int a,int b){ edge[tot]=(Edge){a,b,head[a]}; head[a]=tot++; } void tarjan(int u){//判断可行只需要一个tarjan即可 dfn[u]=low[u]=++Index; sstack[++top]=u; instack[u]=1; for(int i=head[u]; i!=-1; i=edge[i].next){ int v=edge[i].v; if(!dfn[v]){ tarjan(v); low[u]=min(low[u],low[v]); } else if(instack[v]) low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]){ ++cnt; while(1){ int k=sstack[top--]; instack[k]=0; belong[k]=cnt; if(k==u) break; } } } bool solve(int x){ init(); for(int i=1;i<=x;i++){ //if(a[i]+b[i]==2*n-1) continue; add_edge(2*n-1-a[i],b[i]); add_edge(2*n-1-b[i],a[i]); } for(int i=0;i<2*n;i++){ if(!dfn[i]) tarjan(i); } int flag=0; for(int i=0;i<2*n;i++){ if(belong[i]==belong[2*n-1-i]) flag=1; } if(flag) return false; return true; } int main(){ //freopen("in.txt","r",stdin); int t; cin>>t; while(t--){ cin>>n; for(int i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]); int l=1,r=n; while(l<=r){ int mid=(l+r)/2; if(solve(mid)) l=mid+1; else r=mid-1; } cout<<r<<endl; } return 0; }