首页 > 其他分享 >CF1263F Economic Difficulties

CF1263F Economic Difficulties

时间:2024-07-14 22:41:05浏览次数:12  
标签:int rep flow tot Economic rd dep Difficulties CF1263F

我是网络流锰锌,这道题折磨了我很久。

我们发现这里面每条边可选可不选,并且都有一定的限制条件,于是我们的思路可以往网络流方面靠拢。那么题目要求最大化,我们发现用最大流并不好做,于是考虑转化为最小割。其中要割的边就是我们要选的边数。

根据套路很自然的我们先考虑将 \(S\) 向每个点连边,每条边流量为 \(1\),意思是选这条边的代价。思路清晰的,我们考虑有哪些限制。首先,我们选一条连接叶子的边,那么必须将上面的边全部选上,为了满足这个条件,我们把每个条边上面连接的边向自己连边,边权为无限,因为这样如果我们要割掉一条边,那么上面的边显然也要割掉,不然这是无意义的。

然后最难的地方是如何处理电机的两端。我们需要建出两个中至少选择一个的关系。我们可以将图改造一下,把第二棵树代表边的每个点向 \(T\) 连边,边权为 \(1\),再把每个儿子向父亲连边,边权为无限,然后对于电机两端的点,我们将第一棵树的店连向第二棵树的店,流量也为无限。我们发现,这样子建图就非常合理,可以满足需要的关系。

这样子建图的边数仅仅是 \(\mathcal{O}(n)\) 级别的,所以很容易跑过。

代码:

#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i <= r; ++ i)
#define rrp(i, l, r) for (int i = r; i >= l; -- i)
#define eb emplace_back
#define inf 1000000000
#define linf 10000000000000000
#define pii pair <int, int>
using namespace std;
const int N = 5e4 + 5;
inline int rd ()
{
	int x = 0, f = 1;
	char ch = getchar ();
	while (! isdigit (ch)) { if (ch == '-') f = -1; ch = getchar (); }
	while (isdigit (ch)) { x = (x << 1) + (x << 3) + ch - 48; ch = getchar (); }
	return x * f;
}
int n, a, b, p[N], x[N], q[N], y[N], s, t; 

namespace mf
{
  int h[N], to[N], nxt[N], val[N], tot = 1;
  int dep[N];
  queue <int> q;
  void init ()
  {
    rep (i, 0, t) h[i] = 0;
    tot = 1;
  }
  void add (int u, int v, int w)
  {
    to[++ tot] = v;
    val[tot] = w;
    nxt[tot] = h[u];
    h[u] = tot;
    
    to[++ tot] = u;
    val[tot] = 0;
    nxt[tot] = h[v];
    h[v] = tot;
  }
  int bfs ()
  {
    q.push (s);
    rep (i, 0, t) dep[i] = 0;
    dep[s] = 1;
    while (! q.empty ())
    {
      int u = q.front ();
      q.pop ();
      for (int i = h[u]; i; i = nxt[i])
      {
        int v = to[i], w = val[i];
        if (! w || dep[v]) continue;
        dep[v] = dep[u] + 1;
        q.push (v);
      }
    }
    return dep[t];
  }
  int dfs (int u, int g)
  {
    if (u == t) return g;
    int flow = 0;
    for (int i = h[u]; i; i = nxt[i])
    {
      int v = to[i], w = val[i];
      if (! w || dep[v] != dep[u] + 1) continue;
      int tmp = dfs (v, min (g, w));
      flow += tmp, val[i ^ 1] += tmp;
      val[i] -= tmp, g -= tmp;
    }
    if (! flow) dep[u] = 0;
    return flow;
  }
  int solve ()
  {
    int flow = 0;
    while (bfs ()) flow += dfs (s, inf);
    return flow;
  }
}

signed main ()
{
  n = rd ();
  a = rd ();
  rep (i, 2, a)
  {
    int j = rd ();
    if (j == 1) continue;
    mf::add (j, i, inf);
  }
  rep (i, 1, n) x[i] = rd ();
  b = rd ();
  rep (i, 2, b)
  {
    int j = rd ();
    if (j == 1) continue;
    mf::add (i + a, j + a, inf);
  }
  rep (i, 1, n)
  {
    y[i] = rd ();
    mf::add (x[i], y[i] + a, inf);
  }
  t = a + b + 1;
  rep (i, 2, a) mf::add (0, i, 1);
  rep (i, 2, b) mf::add (a + i, t, 1);
  printf ("%d\n", a - 1 + b - 1 - mf::solve ());
}

/*
1
6 2 6
1 2 1 3 2 2 
*/

标签:int,rep,flow,tot,Economic,rd,dep,Difficulties,CF1263F
From: https://www.cnblogs.com/lalaouyehome/p/18302151

相关文章

  • DMSQF ECO2101 Microeconomics
    DMSQF ECO2101 MicroeconomicsCA1 Individual Project - Instructions for StudentsJuly 2024 Semester (BLENDED)CA1 IndividualProjectAssessmentn This CA1 constitutes 30% of the overall ECO2101 Microeconomics course assessment. Rational......
  • Cultural and Economic Decline in New York
     1. Cultural and Economic Decline in New York●  New York has seen a decrease in creative buildings and attractions in architecture, entertainment, music, and sports.●  Compared to other seaport cities, New York no lo......
  • 初中英语优秀范文100篇-099Keep patient when facing difficulties-面对困难时保持耐
    PDF格式公众号回复关键字:SHCZFW099记忆树1IstillrememberthefirsttimeIrodeabicyclewhenIwasseven.翻译我仍然记得我七岁时第一次骑自行车的情景简化记忆自行车句子结构主语I表示我谓语stillremember仍然记得宾语从句thefirsttimeIrodea......
  • 初中英语优秀范文100篇-097-I no longer fear difficulties-我不再害怕困难
    PDF格式公众号回复关键字:SHCZFW097记忆树1Iusedtobeafraidofspeakinginpublic.翻译我曾经害怕在公共场合发言简化记忆发言句子结构主语I表示我谓语usedtobeafraid使用usedto表示过去的习惯或常态,beafraid为系表结构,表示害怕的情感状态宾语of......
  • POLIR-Economics-西方经济学学习经验(转发)
    原文:https://bbs.pinggu.org/thread-894259-1-1.htmlhttps://bbs.pinggu.org/forum-47-1.html西方经济学属于纯理论性的学科,它所包括的知识也基本上是比较模式化的,也就是说,相对于政治经济学它联系实际的东西比较少,能与实际联系起来的地方主要是宏观部分的财政政策、货币政策、通......
  • POLIR-Economics-Microeconomics: 经济模型{静态分析+比较静态分析+动态分析}}@<<西方
    经济理论经济理论是在对现实的经济事物的主要特征和内在联系进行概括和抽象的基础上,对现实的经济事务进行的系统描述;西方经济学家认为由于现实的经济事务是错综复杂的,所以在研究每一个经济事物时,往往要舍弃一些非基本的因素,只就经济事物的基本因素及其相互之间的......
  • POLIR-Economics-Financial Management: 财务管理学
    社会的进步,政治的实践,经济的发展,理论与实践的统一,宏观质变与微观量变是统一的;而社会的发展,与个人生活的提升,在于社会组织的承接,企业/公司作为社会化大生产的主体组织,其生产经营活动充满复杂性,决定了企业管理必须包括多方面的内容,如:销售管理研发管理生产管理设备管理劳......
  • SEE 08 Best alternative & economic life
    Bestalternative&economiclife8.1ProposalRelationshipsIndependentproposal&dependentproposalFormsofdependencyCo-dependent.Mutuallyexclusive.Contingent8.2DependentProposalsCo-DependentProposalswhenselectinganyonefromtha......
  • Notes: Principles of Econometrics / Statistical Method in Economics
    SMENote2Thisnoteisthenoteforstatisticalmethodineconomicscourse.Madeforquickcheckofimportantformulae.kw:xi,residual\(\sum_{i=1}^nx_i......
  • Serverless 遇到 FinOps: Economical Serverless
    Serverless遇到FinOps:EconomicalServerless**摘要:**本文基于FunctionGraph在Serverless领域的FinOps探索和实践,提出业界首个Serverless函数总成本估计模型......