传送门:https://www.hackerrank.com/contests/world-codesprint-11/challenges/hackerland

【题解】

因为加点每次加1个点1条边,所以不会存在一定要经过后加的那些点才能到达的情况。

直接把最后的图建出来,tarjan缩强联通分量+拓扑排序处理连通性。

拿个bitset维护拓扑排序过程即可。复杂度O(n^2/32)

# include <queue>
# include <bitset>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + ;
const int mod = 1e9+; # define RG register
# define ST static int n, m, head[M], nxt[M], to[M], tot=, c; struct Graph {
int head[M], nxt[M], to[M], tot;
inline void set() {tot = ;}
inline void add(int u, int v) {
++tot; nxt[tot] = head[u]; head[u] = tot; to[tot] = v;
}
}G1, G; struct pa {
int op, a, b;
}q[M]; int DFN = , dfn[M], low[M], bel[M], scc = ;
int st[M], stn=;
bool ins[M];
inline void tarjan(int x) {
dfn[x] = low[x] = ++DFN;
st[++stn] = x;
ins[x] = ;
for (int i=G1.head[x]; i; i=G1.nxt[i]) {
if(!dfn[G1.to[i]]) {
tarjan(G1.to[i]);
low[x] = min(low[x], low[G1.to[i]]);
} else if(ins[G1.to[i]]) low[x] = min(low[x], dfn[G1.to[i]]);
}
if(low[x] == dfn[x]) {
int t = ; ++scc;
while(t != x) {
t = st[stn];
--stn; ins[t] = ;
bel[t] = scc;
}
}
} bitset<> bs[];
int out[M], ans[M], an=;
queue<int> qu; int main() {
G1.set(), G.set();
cin >> n >> m; c = n;
for (int i=, u, v; i<=m; ++i) {
scanf("%d%d", &u, &v);
G1.add(u, v);
}
cin >> m;
for (int i=; i<=m; ++i) {
scanf("%d%d%d", &q[i].op, &q[i].a, &q[i].b);
if(q[i].op==) {
++c;
if(q[i].b) G1.add(c, q[i].a);
else G1.add(q[i].a, c);
}
}
for (int i=; i<=c; ++i) if(!dfn[i]) tarjan(i); // for (int i=1; i<=c; ++i) printf("%d\n", bel[i]); for (int i=; i<=c; ++i) {
bs[bel[i]][bel[i]] = ;
for (int j=G1.head[i]; j; j=G1.nxt[j])
if(bel[i] != bel[G1.to[j]]) {
bs[bel[i]][bel[G1.to[j]]] = ;
// printf("%d %d\n", bel[i], bel[G1.to[j]]);
out[bel[i]] ++;
G.add(bel[G1.to[j]], bel[i]);
}
}
for (int i=; i<=scc; ++i)
if(!out[i]) qu.push(i);
while(!qu.empty()) {
int top = qu.front(); qu.pop();
for (int i=G.head[top]; i; i=G.nxt[i]) {
--out[G.to[i]];
bs[G.to[i]] |= bs[top];
if(out[G.to[i]] == ) qu.push(G.to[i]);
}
}
// for (int i=1; i<=n; ++i, cout << endl)
// for (int j=1; j<=n; ++j) cout << bs[i][j] << ' '; for (int i=m; i; --i)
if(q[i].op == ) ans[++an] = bs[bel[q[i].a]][bel[q[i].b]]; for (int i=an; i; --i) puts(ans[i] ? "Yes" : "No"); return ;
}

Hackerrank [World CodeSprint 11] City Construction的更多相关文章

  1. Official Program for CVPR 2015

    From:  http://www.pamitc.org/cvpr15/program.php Official Program for CVPR 2015 Monday, June 8 8:30am ...

  2. Web 入门之 XML

      160916   1. 什么是XML?   XML 是 EXtensible Markup Language 的缩写,称为可扩展标记语言,所谓可扩展指用户可根据XML规则自定义标记.例子1-1 = ...

  3. Android 三级联动选择城市+后台服务加载数据库

    技术渣,大家将就着看 首先我们需要一个xml数据保存到数据库,这里我从QQ下面找到一个loclist.xml文件 <CountryRegion Name="中国" Code= ...

  4. 新浪微博SDK开发(1):总述

    花了几天时间,消耗了九牛六虎之力,新浪微博大部分API已经封装,但有部分API实在太难封装. 说起这封装,我必须严重地.从人品和技术层面鄙视一下新浪的程序员,实在太菜了.估计菜鸟都被大企业吸收了,菜到 ...

  5. 世界城市 XML

    下载地址:http://www.qlcoder.com/uploads/dd01140921/147988679320159.xml <Location> <CountryRegio ...

  6. R语言 recommenderlab 包

    recommend li_volleyball 2016年3月20日 library(recommenderlab) ## Warning: package 'recommenderlab' was ...

  7. R语言 推荐算法 recommenderlab包

    recommend li_volleyball 2016年3月20日 library(recommenderlab) library(ggplot2) # data(MovieLense) dim(M ...

  8. ajax读取XML文本(如读取城市)

    //加载城市 function loadArea_pep() { $.ajax({ url: "/xmlFile/crty.xml", success: function (res ...

  9. OAuth2.0 微博登陆网站功能的实现(一)获取用户授权及令牌 Access Token

    在登陆一些网站的时候,可以选择登陆方式为第三方登陆,例如微博登陆,以爱奇艺为例,进入首页,点击 ”登陆“,会弹出登录框: 除了本站登陆外,还可以选择其他第三方登陆,比如微博登陆.QQ 登陆.微信登陆等 ...

随机推荐

  1. Django-Content-type用法

    from django.db import models from django.contrib.contenttypes.models import ContentType from django. ...

  2. 【jQuery】 资料

    [jQuery] 资料 1. 选择器 http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp 2. 事件 http://www.w3sch ...

  3. result returns more than one elements此种错误,解决

    场景:公司产品开发完成后,接入第三方厂商,在进行接口联调的时候出现此问题.此接口报文中的每一个数据都要进行校验,有些是与已经存入产品数据库中的数据进行对比,看是否存在. 问题:在测试中,有些测试没有问 ...

  4. Linux系统安装jdk后出现无法执行binary 文件的错误解决

    这是由于jdk版本的问题,针对Linux系统,Oracle提供了 ARM 的32jdk和64位jdk , 但是也提供了类似这样jdk-8u191-linux-i586.tar.gz32或64位的jdk ...

  5. 当因式分解遇见近邻:一种多层面协同过滤模型(SVD++)

    本文地址:https://www.cnblogs.com/kyxfx/articles/9392086.html actorization Meets the Neighborhood: a Mult ...

  6. LeetCode 83 —— 删除排序链表中的重复元素

    1. 题目 2. 解答 从前向后遍历链表,如果下一个结点的值和当前结点的值相同,则删除下一个结点,否则继续向后遍历. /** * Definition for singly-linked list. ...

  7. get? post? put? delete? head? trace? options? http请求方法

    http1.1协议里面定义了八种请求方法: get:用作获取,读取数据 post:向指定的资源提交数据 put:更新,向指定的资源上传一个内容,比如说:更新一个用户的头像或者替换掉已有的一个视频 de ...

  8. lintcode-136-分割回文串

    136-分割回文串 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 样例 给出 s = "aab",返回 [ ["aa&q ...

  9. [剑指Offer] 42.和为S的两个数字

    题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. [思 ...

  10. 微服务日志监控与查询logstash + kafka + elasticsearch

    使用 logstash + kafka + elasticsearch 实现日志监控 https://blog.csdn.net/github_39939645/article/details/788 ...