首页 > 其他分享 >Kattis - A Complex Problem (The 2023 ICPC Rocky Mountain Regional Contest)

Kattis - A Complex Problem (The 2023 ICPC Rocky Mountain Regional Contest)

时间:2023-11-11 09:02:58浏览次数:50  
标签:Mountain Rocky Kattis int comp SCC edges ans adj

Intro

This was one of the problems I didn't do during the regional contest. One of my teammates solved it.

Observation

There are few things to note.

  • First type of notation: subset means that A $ \subset $ B, but there can be cases that subset forms a SCC s.t. all nodes in the SCC are same classification class.
  • Second type: this means A is different class from B, with 1 less problem. But consider a case A is both type2 to B and C, then B and C may or may not be the same thing. Therefore, the size of the class can be used to determine if they can be same or not. Which naturally leads to DP on DAG (which can be determined by topological sort).

Solution

Consider all type1 as edges with weight 0, all type2 as edges with weight 1. Run a SCC code to find all strongly connected components. The maximum possible number of classes = the number of SCC in the graph. Now we consider the minimum case. Since KACTL SCC code traverses the components in reverse topological sort order, we can do a linear scan from last component to the first to get a topological order. Having a dp table set up with size = # of components in the graph. Then we do a DP on DAG and the maximum value in the dp table is the minimal number of classification class we can have.

Remark

  • KACTL SCC code traverses in reverse topological order.
  • A single node is always a SCC.
  • Lambda Function is used for kactl scc.
[&](vi &v) {...}

Code

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

vi val, comp, z, cont;
int Time, ncomps;
template<class G, class F> int dfs(int j, G& g, F& f) {
	int low = val[j] = ++Time, x; z.push_back(j);
	for (auto e : g[j]) if (comp[e] < 0)
		low = min(low, val[e] ?: dfs(e,g,f));

	if (low == val[j]) {
		do {
			x = z.back(); z.pop_back();
			comp[x] = ncomps;
			cont.push_back(x);
		} while (x != j);
		f(cont); cont.clear();
		ncomps++;
	}
	return val[j] = low;
}
template<class G, class F> void scc(G& g, F f) {
	int n = sz(g);
	val.assign(n, 0); comp.assign(n, -1);
	Time = ncomps = 0;
	rep(i,0,n) if (comp[i] < 0) dfs(i, g, f);
}

map<string, int> mp;
int N = 0;
int id(string s) {
    if (mp.count(s)) return mp[s];
    mp[s] = N++;
    return mp[s];
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);
    int n, m;
    cin >> n >> m;
    vector<tuple<int, int, int>> edges;
    rep(i, 0, n) {
        string a, b;
        cin >> a >> b;
        int u = id(a);
        int v = id(b);
        edges.pb({u, v, 0});
    }
    rep(i, 0, m) {
        string a, b;
        cin >> a >> b;
        int u = id(a);
        int v = id(b);
        edges.pb({u, v, 1});
    }
    vector<vi> adj(N);
    for (auto &[a, b, _] : edges) {
        adj[a].pb(b);
    }
    scc(adj, [&](vi &v){return;});
    vector<vector<pii>> t_adj(N);

    for (auto &[a, b, _] : edges) {
        if (comp[a] == comp[b]) continue;
        t_adj[comp[a]].pb({comp[b], _});
    }

    vi ans(ncomps, -1);
    int mn = 0;
    for (int i = ncomps -1; i >= 0; i--) {
        if (ans[i] == -1) ans[i] = 1;
        mn = max(mn, ans[i]);
        for (auto &[b, kind] : t_adj[i]) {
            int cand = ans[i] + kind;
            ans[b] = max(ans[b], cand);
        }
    }
    cout << mn << ' ' << ncomps << '\n';
    return 0;
}

标签:Mountain,Rocky,Kattis,int,comp,SCC,edges,ans,adj
From: https://www.cnblogs.com/qiyu0224/p/17825481.html

相关文章

  • rocky linux v9.2 网络配置
         ......
  • angie rocky docker 镜像问题 二
    我以前说过关于angierockydocker镜像的问题,今天官方已经修复了,修复方法与我介绍的是类似的参考官方修复方案通过dive工具查看到的 nginx参考资料https://github.com/webserver-llc/angie/issues/54https://www.cnblogs.com/rongfengliang/p/17807329.html......
  • angie rocky docker 镜像问题
    angierockydocker在构建的时候似乎有一些问题,启动的时候会有问题异常信息angie:[emerg]open()"/run/angie/angie.pid"failed(2:Nosuchfileordirectory)解决方法自己构建一个镜像,对于缺少的文件夹进行创建FROMdocker.angie.software......
  • 第一周 安装rocky 8.5
    1、下载RockyLinux官方镜像8.5  1.1打开网址直接下载http://dl.rockylinux.org/vault/rocky/8.5/isos/x86_64/Rocky-8.5-x86_64-dvd1.iso2.创建虚拟机导入iso文件,进入RockyLinux的初始安装界面,选择installRockyLinux8后,按下回车键enter,开始安装RockyLinux。  ......
  • Cenots7.8 openstack rocky及mellanox cx5智能网卡基础环境搭建
    一.网卡SRIOV及虚拟化配置1. Mellanox网卡SRIOV开启配置网卡安装之后需要到官方网站去下载相关网卡的驱动,然后才能进行下面的配置,本次实验环境使用的是centos7.8,自带了MellanoxCX系列网卡的驱动。 所以网卡驱动安装的部分先不涉及,需要下载的可以到nvdia的官方网站去下载:h......
  • Rocky Linux 8配置时间同步服务 chrony
    我们需要再单独去安装 dnfinstall-ychrony,只需要配置对应的时间同步服务器即可。服务器配置#Usepublicserversfromthepool.ntp.orgproject.#Pleaseconsiderjoiningthepool(http://www.pool.ntp.org/join.html).server10.32.186.70iburst  //添加时间服......
  • rocky8 服务器的时区设置为“上海”
    timedatectlset-timezoneAsia/Shanghai查看当前的系统时区首先查看当前的系统时区,确认系统时区执行date命令。也可以使用timedatectlstatus命令查看当前的系统时区。timedatectl命令指定”set-timezone”选项可修改系统时区,将时区设定为“Asia/Shanghai”时,执行 “ti......
  • ROCKY 8 搭建本地yum源及配置局域网公用
    1.上传完整镜像文件 CRT软件,ALT+P打开上传界面,put命令进行上传,windows端命令在前面加L。1.将iso文件上传至/opt下。2.创建挂载目录3.挂载iso文件mount-oloop/opt/Rocky-8.4-x86_64-dvd1.iso/mnt/cdrom/4.修改配置yum源vim/etc/yum.repos.d/Rocky-Media.repo(修改......
  • CERC2014 Mountainous landscape
    1ay1D。这是一个跑不过双\(\log\)的单\(\log\)做法。考虑双\(\log\)做法是怎么做的。令\(a_i(1\lei\len)\)为给定的\(x\)坐标递增的点序列,开一棵线段树维护区间上凸壳,第\(i\)次查询相当于在\([i+2,n]\)区间组成的点的上凸壳中,找到一个在经过\(a_i,a_{i+1}\)......
  • CF1852C Ina of the Mountain
    *2400https://codeforces.com/problemset/problem/1852/C如果没有\(\modk\)的限制的话,我们都会做,因为都是正数,那么\(\sum_i^nd_i>0\),因此,答案即为\(\sum[d_i>0]d_i\)。但是现在多了一个操作,即为区间加\(k\),那么转到差分数组就是\(d_l+k,d_r-k\),且该操作不花费。观察,差......