2020/8/28
英语四级
基环树学习
5题





#include"stdio.h"
#include"string.h"
#include"stack"
#include"map"
#include"math.h"
#include"iostream"
#include"vector"
#include"queue"
#include"algorithm"
using namespace std;
#define OK printf("\n");
#define Debug printf("this_ok\n");
#define inf 1e9
#define INF 1e18
typedef long long ll;
#define scanll(a,b) scanf("%lld%lld",&a,&b);
#define scanl(a) scanf("%lld",&a);
#define printl(a,b) if(b == 0) printf("%lld ",a); else printf("%lld\n",a);
#define print_int(a,b) if(b == 0) printf("%d ",a); else printf("%d\n",a);
typedef pair<int,int> PII;
inline int read(){
int s = 0, w = 1; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar(); }
return s * w;
}
const ll mod = 998244353;
const int N = 2000;
const double pi = acos(-1);
int n,m;
ll head[N],ver[N << 1],Next[N << 1],tot;
int match[N],visit[N],ans1[N];
void add(int x,int y){
ver[++ tot] = y; Next[tot] = head[x]; head[x] = tot;
}
bool dfs(int x){
for(ll i = head[x];i; i = Next[i]){
int y = ver[i];
if(visit[y]) continue;
visit[y] = 1;
if(!match[y] || dfs(match[y])) {
match[y] = x; return true;
}
}
return false;
}
int main(){
scanf("%d%d",&n,&m);
for(int i = 1; ; i ++){
int x = read(),y = read();
if(x == -1) break;
add(x,y); //add(y,x);
}
int ans = 0;
for(int i = 1; i <= m; i ++){
memset(visit,0,sizeof(visit));
if(dfs(i)) ans ++;
}
for(int i = n + 1; i <= m; i ++){
ans1[match[i]] = i;
}
printf("%d\n",ans);
for(int i = n + 1; i <= m; i ++){
if(match[i] == 0) continue;
printf("%d %d\n",match[i],i);
}
}
/*
3
1 2 3
2
2 6
*/
#include"stdio.h"
#include"string.h"
#include"stack"
#include"map"
#include"math.h"
#include"iostream"
#include"vector"
#include"queue"
#include"algorithm"
using namespace std;
#define OK printf("\n");
#define Debug printf("this_ok\n");
#define INF 1e18
typedef long long ll;
#define scanll(a,b) scanf("%lld%lld",&a,&b);
#define scanl(a) scanf("%lld",&a);
#define printl(a,b) if(b == 0) printf("%lld ",a); else printf("%lld\n",a);
#define print_int(a,b) if(b == 0) printf("%d ",a); else printf("%d\n",a);
typedef pair<int,int> PII;
inline int read(){
int s = 0, w = 1; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar(); }
return s * w;
}
const ll mod = 998244353;
const int N = 50010,M = 300010;
const double pi = acos(-1);
const int inf = 1 << 29;
const int dirx[4] = {-1,0,1,0};
const int diry[4] = {0,1,0,-1};
int n,m,t,s,tot;
ll maxflow,sum;
int head[N],ver[M],Next[M],edge[M],d[N];
queue<int> q;
void add(int x,int y,int z){
ver[++ tot] = y; Next[tot] = head[x]; edge[tot] = z; head[x] = tot;
ver[++ tot] = x; edge[tot] = 0; Next[tot] = head[y]; head[y] = tot;
}
bool bfs(){
memset(d,0,sizeof(d));
while(q.size())q.pop();
q.push(s); d[s] = 1;
while(q.size()){
int x = q.front(); q.pop();
for(int i = head[x]; i; i = Next[i])
if(edge[i] && !d[ver[i]]){
q.push(ver[i]); d[ver[i]] = d[x] + 1;
if(ver[i] == t) return 1;
}
}
return 0;
}
int dinic(int x,ll flow){
if(x == t) return flow;
ll rest = flow,k;
for(int i = head[x]; i && rest; i = Next[i]){
if(edge[i] && d[ver[i]] == d[x] + 1){
k = dinic(ver[i],min(rest,(ll)edge[i]));
if(!k) d[ver[i]] = 0;
edge[i] -= k;
edge[i ^ 1] += k;
rest -= k;
}
}
return flow - rest;
}
int main(){
n = read(),m = read();s = n * m + m + 1; t = n * m + m + 2;
tot = 1;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++){
int x = read(); sum += x;
if(((i + j) & 1) == 0){
add(s,i * m + j,x);
} else add(i * m + j,t,x);
if(((i + j) & 1) == 0)
for(int k = 0; k < 4; k ++){
int X = i + dirx[k];
int Y = j + diry[k];
if(X < 1 || X > n || Y < 1 || Y > m) continue;
add(i * m + j,X * m + Y,inf);
}
}
ll flow = 0;
while(bfs())
while(flow = dinic(s,inf)) maxflow += flow;
printf("%lld\n",sum - maxflow);
}
/*
3
1 2 3
2
2 6
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include"set"
#include"map"
using namespace std;
typedef long long ll;
inline int read(){
int s = 0, w = 1; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar(); }
return s * w;
}
const int inf=1e9;
const int N=300,M=N*N+N+7,E=500005;
int ver[E], edge[E], Next[E], head[E];
int cost[E],d[M];
int incf[M], pre[M], v[M];
ll ans;
int n, k, tot, s, t, maxflow,m,q[E];
int store1[N],store2[N],store[N][N];
void add(int x, int y, int z, int c) {
// 正向边,初始容量z,单位费用c
ver[++tot] = y, edge[tot] = z, cost[tot] = c;
Next[tot] = head[x], head[x] = tot;
// 反向边,初始容量0,单位费用-c,与正向边“成对存储”
ver[++tot] = x, edge[tot] = 0, cost[tot] = -c;
Next[tot] = head[y], head[y] = tot;
}
bool spfa() {
queue<int> q;
for(int i = 0; i <= n + m + m + n; i ++) d[i] = inf;
memset(v, 0, sizeof(v));
q.push(s); d[s] = 0; v[s] = 1; // SPFA 求最长路
incf[s] = 1 << 30; // 增广路上各边的最小剩余容量
while (q.size()) {
int x = q.front(); v[x] = 0; q.pop();
for (int i = head[x]; i; i = Next[i]) {
if (!edge[i]) continue; // 剩余容量为0,不在残量网络中,不遍历
int y = ver[i];
if (d[y]>d[x] + cost[i]) {
d[y] = d[x] + cost[i];
incf[y] = min(incf[x], edge[i]);
pre[y] = i; // 记录前驱,便于找到最长路的实际方案
if (!v[y]) v[y] = 1, q.push(y);
}
}
}
if (d[t] == inf) return false; // 汇点不可达,已求出最大流
return true;
}
// 更新最长增广路及其反向边的剩余容量
void update() {
int x = t;
while (x != s) {
int i = pre[x];
edge[i] -= incf[t];
edge[i ^ 1] += incf[t]; // 利用“成对存储”的xor 1技巧
x = ver[i ^ 1];
}
maxflow += incf[t];
ans += d[t] * incf[t];
//printf("%d %d sd\n ",d[t],incf[t]);
}
int main() {
n = read(); m = read(); tot = 1;
t = n + m + 2;
s = n + m + 1;
for(int i = 1; i <= n; i ++) {
scanf("%d",&store1[i]);
add(s,i,store1[i],0);
}
for(int i = 1; i <= m; i ++) {
scanf("%d",&store2[i]);
add(i + n,t,store2[i],0);
}
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
int x; scanf("%d",&x);
store[i][j] = x;
add(i,j + n,inf,x);
}
}
maxflow = ans = 0;
while(spfa()){
update();
//printf("%lld %lld\n",maxflow,ans);
}
printf("%lld\n",ans);
memset(head,0,sizeof(head)); tot = 1;
for(int i = 1; i <= n; i ++) {
add(s,i,store1[i],0);
}
for(int i = 1; i <= m; i ++) {
add(i + n,t,store2[i],0);
}
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
add(i,j + n,inf,-store[i][j]);
}
}
maxflow = ans = 0;
while(spfa()){
update();
// printf("%lld %lld\n",maxflow,ans);
}
printf("%lld\n",-ans);
}
#include"stdio.h"
#include"string.h"
#include"stack"
#include"map"
#include"math.h"
#include"iostream"
#include"vector"
#include"queue"
#include"algorithm"
using namespace std;
#define OK printf("\n");
#define Debug printf("this_ok\n");
#define inf 1e9
#define INF 1e18
typedef long long ll;
#define scanll(a,b) scanf("%lld%lld",&a,&b);
#define scanl(a) scanf("%lld",&a);
#define printl(a,b) if(b == 0) printf("%lld ",a); else printf("%lld\n",a);
#define print_int(a,b) if(b == 0) printf("%d ",a); else printf("%d\n",a);
typedef pair<int,int> PII;
inline int read(){
int s = 0, w = 1; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar(); }
return s * w;
}
const ll mod = 998244353;
const int N = 300100;
const double pi = acos(-1);
int n,m;
int head[N],ver[N << 1],Next[N << 1],tot;
int d[N],a[N],far[N];
int k,t;
int id[N];
int f[N][20];
void add(int x,int y){
ver[++ tot] = y; Next[tot] = head[x]; head[x] = tot;
}
void bfs(int x){
queue<int> Q;Q.push(x);
d[x] = 1;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = head[x]; i; i = Next[i]){
int y = ver[i];
if(d[y]) continue;
d[y] = d[x] + 1;
f[y][0] = x;
for(int j = 1; j <= t; j ++)
f[y][j] = f[f[y][j - 1]][j - 1];
Q.push(y);
}
}
}
int lca(int x,int y){
if(d[x] > d[y]) swap(x,y);
for(int i = t; i >= 0; i --)
if(d[f[y][i]] >= d[x]) y = f[y][i];
if(x == y) return x;
for(int i = t; i >= 0; i --)
if(f[x][i] != f[y][i]) x = f[x][i],y = f[y][i];
return f[x][0];
}
int cmp(int x,int y){
return d[x] > d[y];
}
int main()
{
n = read(); m = read();
t = (int)(log(n) / log(2)) + 1;
far[1] = 1;
for(int i = 1; i < n; i ++){
int x = read(),y = read();
add(x,y); add(y,x);
}
bfs(1);
while(m --){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
int x = lca(a, b), y = lca(a, c), z = lca(b, c);
if(x == y) swap(y, z);
if(d[x] < d[y]) swap(x, y);
int sum = d[a] + d[b] + d[c] - d[y] * 3 - (d[x] - d[y]);
printf("%d %d\n", x, sum);
}
}
/*
3
1 2 3
2
2 6
*/
#include"stdio.h"
#include"string.h"
#include"stack"
#include"map"
#include"math.h"
#include"iostream"
#include"vector"
#include"queue"
#include"algorithm"
using namespace std;
#define OK printf("\n");
#define Debug printf("this_ok\n");
#define inf 1e9
#define INF 1e18
typedef long long ll;
#define scanll(a,b) scanf("%lld%lld",&a,&b);
#define scanl(a) scanf("%lld",&a);
#define printl(a,b) if(b == 0) printf("%lld ",a); else printf("%lld\n",a);
#define print_int(a,b) if(b == 0) printf("%d ",a); else printf("%d\n",a);
typedef pair<int,int> PII;
inline int read(){
int s = 0, w = 1; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar(); }
return s * w;
}
const ll mod = 998244353;
const int N = 300100;
const double pi = acos(-1);
int n,m,match[N];
bool G[250][250];
bool vis[N],succ[N];
int hide[N];
bool dfs(int x){
for(int i = 1; i <= n; i ++){
if(G[x][i] && !vis[i]){
vis[i] = true;
if(!match[i] || dfs(match[i])){
match[i] = x; return true;
}
}
}
return false;
}
int main(){
n = read(); m = read();
for(int i = 1; i <= m; i ++){
int x = read(),y = read();
G[x][y] = 1;
}
for(int i = 1; i <= n; i ++) G[i][i] = 1;
for(int k = 1; k <= n; k ++)
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
G[i][j] |= G[i][k] & G[k][j];
for(int i = 1; i <= n; i ++) G[i][i] = 0;
int ans = n;
for(int i = 1; i <= n; i ++){
memset(vis,0,sizeof(vis));
ans -= dfs(i);
}
printf("%d\n",ans);
}
浙公网安备 33010602011771号