Codeforces 1009G Allowed Letters 最大流转最小割 sosdp
最直观的想法是贪心取, 然后网络流取check可不可行, 然后T了。
想到最大流可以等于最小割, 那么我们状压枚举字符代表的6个点连向汇点是否断掉,
然后再枚举64个本质不同的位置, 是否需要切段原点联想它的边, 单次check复杂度64 * 64
用sosdp能优化到64 * 6
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end() using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, m, mask[N], c[N], cnt[N], sos[N];
char s[N], t[N], ans[N]; bool check() {
int sum = , ret = inf;
for(int i = ; i < ; i++) sum += c[i];
for(int i = ; i < ; i++) sos[i] = cnt[i];
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
if(j >> i & ) sos[j] += sos[j ^ ( << i)];
for(int s1 = ; s1 < ; s1++) {
int tmp = ;
for(int i = ; i < ; i++)
if(s1 >> i & ) tmp += c[i];
tmp += sum - sos[s1];
chkmin(ret, tmp);
}
return sum == ret;
} inline int getId(char c) {
return c - 'a';
} int main() {
scanf("%s", s + );
n = strlen(s + );
for(int i = ; i <= n; i++) c[getId(s[i])]++;
scanf("%d", &m);
while(m--) {
int p; scanf("%d%s", &p, t + );
for(int i = ; t[i]; i++) mask[p] |= << getId(t[i]);
}
for(int i = ; i <= n; i++) {
if(!mask[i]) mask[i] = ;
cnt[mask[i]]++;
}
for(int i = ; i <= n; i++) {
bool flag = false;
for(int j = ; j < ; j++) {
if(c[j] && mask[i] >> j & ) {
c[j]--;
cnt[mask[i]]--;
flag = check();
if(flag) {
ans[i] = 'a' + j;
break;
}
c[j]++;
cnt[mask[i]]++;
}
}
if(!flag) return puts("Impossible"), ;
}
ans[n + ] = '\0';
puts(ans + );
return ;
} /*
*/
Codeforces 1009G Allowed Letters 最大流转最小割 sosdp的更多相关文章
- Codeforces 1009G Allowed Letters FMT,二分图,二分图匹配,霍尔定理
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1009G.html 题目传送门 - CF1009G 题意 给定一个长度为 $n$ 的字符串 $s$ .并给定 ...
- Codeforces 628F 最大流转最小割
感觉和昨天写了的题一模一样... 这种题也能用hall定理取check, 感觉更最小割差不多. #include<bits/stdc++.h> #define LL long long # ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E - Goods transportation 最大流转最小割转dp
E - Goods transportation 思路:这个最大流-> 最小割->dp好巧妙哦. #include<bits/stdc++.h> #define LL long ...
- 【bzoj1001】【最短路】【对偶图】【最大流转最小割】狼抓兔子题解
[BZOJ1001]狼抓兔子 1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 18872 Solved ...
- Codeforces 724E Goods transportation(最小割转DP)
[题目链接] http://codeforces.com/problemset/problem/724/E [题目大意] 每个城市有pi的物品可以运出去卖,si个物品可以买, 编号小的城市可以往编号大 ...
- Codeforces 786E. ALT 最小割+倍增
E. ALT http://codeforces.com/problemset/problem/786/E 题意: 给出一棵 n 个节点的树与 m 个工人.每个工人有一条上下班路线(简单路径),一个工 ...
- Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)
Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...
- Yet Another Maxflow Problem CodeForces - 903G (最小割,线段树)
大意: 两个n元素集合$A$, $B$, $A_i$与$A_{i+1}$连一条有向边, $B_i$与$B_{i+1}$连一条有向边, 给定$m$条从$A_i$连向$B_j$的有向边, 每次询问修改$A ...
- CodeForces E. Goods transportation【最大流+dp最小割】
妙啊 首先暴力建图跑最大流非常简单,s向每个i连流量为p[i]的边,每个i向t连流量为s[i]的边,每个i向j连流量为c的边(i<j),但是会又T又M 考虑最大流=最小割 然后dp求最小割,设f ...
随机推荐
- Django自带的用户认证auth模块
一.介绍 基本上在任何网站上,都无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能. 使用Django,我们可以不需要自己写这些功能,因为Dj ...
- Ubuntu下基于Virtualenv构建Python开发环境
1.安装virtualenv并建立虚拟环境 1).更新pip版本 sudo pip install --upgrade pip 如果出现如下异常: File , in <module> f ...
- Could not find package vendor/name in a version matching v-Number 是坑!
当我遇到这个问题的时候曾去发布了issue -https://github.com/composer/packagist/issues/934 主要的问题是,composer require vend ...
- 初学cdq分治学习笔记(可能有第二次的学习笔记)
前言骚话 本人蒟蒻,一开始看到模板题就非常的懵逼,链接,学到后面就越来越清楚了. 吐槽,cdq,超短裙分治....(尴尬) 正片开始 思想 和普通的分治,还是分而治之,但是有一点不一样的是一般的分治在 ...
- class09
class09 四川菜很辣. Sichuan cuisine is very spicy. 那个汤是凉的. That soup is cold. 这茶很烫. This tea is very hot. ...
- boost库在windows上的安装
下载源码boost_1_70_0.zip 1.cmd进入boost源码包,运行bootstrap.bat生成bjam.exe 2.运行bjam.exe搞定 在visual studio配置 1. 项目 ...
- phpmyadmin低权限getshell
账号:‘localhost’@'@” 密码:为空 可获得一个低权限账号 利用方法: Mysql可以把指定的文件写进表 CREATE TABLE `test`.`a` (`a1` TEXT NOT NU ...
- 使用FFMPEG进行一些视频处理(C#)视频合并、转码、获取时长
FFMPEG的强大无需多说,举几个用到的功能,直接贴代码了 还有更多命令用到时搜索即可 视频转码 ) { var args = "-y -i {0} -vcodec copy {1}&quo ...
- NodeJS跨域问题
const express = require('express'), app = express(), router = express.Router(), bodyParser = require ...
- Form -------- 使用
Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 1.创建Form类 from django.f ...