zoukankan      html  css  js  c++  java
  • CF568C New Language 2-SAT

    CF568C New Language 2-SAT

    题意

    分析:

    我之前可能学了个假的2-SAT,我一直不知道缩完点之后还要DFS

    对于这个题按照要求建好图之后

    • tips:对于 (1 v 2 c) 我们需要建两条边(1 o 2+n) 和 $ 2 o 1+n $ 表示 1 选 (v) 时 2 必须选 (c),2选 (c) 时1只能选 (c)

    我们暴力从后向前枚举更改一个位置使得字典序变大,对于更改的位置 (x) ,1到 (x-1) 都使得字典序不小于给定串,(x+1)(n) 直接取合法的字典序最小的方案

    代码:

    #include<bits/stdc++.h>
    
    using namespace std;
    
    namespace zzc
    {
    	const int maxn = 1e5+5;
    	int n,m,cnt=0,idx;
    	int f[maxn][2],head[maxn],dfn[maxn],fa[maxn];
    	
    	struct edge
    	{
    		int to,nxt;
    	}e[maxn<<2];
    	
    	void add(int u,int v)
    	{
    		e[++cnt].to=v;
    		e[cnt].nxt=head[u];
    		head[u]=cnt;
    	}
    	
    	void dp(int rt,int x)
    	{
    		int s,t,tmp;
    		s=f[x][0],t=0;
    		for(int i=fa[x];i!=rt;i=fa[i])
    		{
    			tmp=max(s,t);
    			t=f[i][1]+s;
    			s=f[i][0]+tmp;
    		}
    		f[rt][1]+=s;
    		s=t=0;
    		for(int i=x;i!=rt;i=fa[i])
    		{
    			tmp=max(s,t);
    			t=f[i][1]+s;
    			s=f[i][0]+tmp;
    		}
    		f[rt][0]+=max(s,t);
    	}
    	
    	void dfs(int u)
    	{
    		dfn[u]=++idx;
    		for(int i=head[u];i;i=e[i].nxt)
    		{
    			int v=e[i].to;
    			if(!dfn[v]&&v!=fa[u])
    			{
    				fa[v]=u;
    				dfs(v);
    			}
    		}
    		for(int i=head[u];i;i=e[i].nxt)
    		{
    			int v=e[i].to;
    			if(fa[v]!=u&&dfn[v]>dfn[u]) dp(u,v);
    		}
    	}
    	
    	void work()
    	{
    		int a,b;
    		ios::sync_with_stdio(false);
    		cin>>n>>m;
    		for(int i=1;i<=m;i++)
    		{
    			cin>>a>>b;
    			add(a,b);add(b,a);
    		}
    		for(int i=1;i<=n;i++) cin>>f[i][1];
    		dfs(1);
    		printf("%d
    ",max(f[1][0],f[1][1]));
    	}
    
    }
    
    int main()
    {
    	zzc::work();
    	return 0;
    }
    
    
  • 相关阅读:
    c++ time_t
    sql 一些题目
    vc 找到一个或多个多重定义的符号
    c++ json 详解
    c++ json cpp
    C++ string(转)
    java web 复选框checked
    20_采用ContentProvider对外共享数据
    16_采用SharedPreferences保存用户偏好设置参数
    android开发 eclipse alt+”/”自动提示失效
  • 原文地址:https://www.cnblogs.com/youth518/p/13904633.html
Copyright © 2011-2022 走看看