Codeforces Round #503 (by SIS, Div. 2)B 1020B Badge (拓扑)
题目大意:每个同学可以指定一个人,然后构成一个有向图。1-n次查询,从某个人开始并放入一个东西,然后循环,直到碰到一个人已经放过了,就输出。
思路:直接模拟就可以了,O(n^2) 但是O(n)也可以实现, 不是太懂大神的思路。
初始化ans[i] = i, 一个点能被输出的话就是 ans[i] = i (可以模拟一下),这个ans是如何算的呢。大神用了topo排序。 记录入度数,然后把入读为0放入队列,取出来然后连接点入度--, ans[i] = p[i] ( p[i] 是 i 指定的人 )直到队列为空。
O(n^2)代码:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF =0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+;
//head
bool vis[ + ];
vector<int> G[ + ];
int main() {
int n, x;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &x);
G[i].push_back(x);
}
for(int i = ; i <= n; i++) {
mem(vis, false);
vis[i] = true;
int net = G[i][];
while(vis[net] != true) {
vis[net] = true;
net = G[net][];
}
printf("%d ", net);
}
}
O(n)代码:
#include<iostream>
#include<queue>
using namespace std;
const int maxn = + ;
int p[maxn], ans[maxn], deg[maxn]; int res(int i) {//比较神奇 也就是循环
return i == ans[i] ? i : res(p[i]);
} int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &p[i]);//每个点指定的点
deg[p[i]]++;//入读++
}
queue<int> q;
for(int i = ; i <= n; i++) {//初始化ans
ans[i] = i;
if(deg[i] == )//入度为0 进队列
q.push(i);
}
while(!q.empty()){//topo
int u = q.front(); q.pop();
deg[p[u]]--;
ans[u] = p[u];//这里不是太懂(模拟一下比较好)
if(deg[p[u]] == )
q.push(p[u]);
}
for(int i = ; i <= n; i++)
printf("%d ", res(i));
return ;
}
Codeforces Round #503 (by SIS, Div. 2)B 1020B Badge (拓扑)的更多相关文章
- Codeforces Round #503 (by SIS, Div. 2) Solution
从这里开始 题目列表 瞎扯 Problem A New Building for SIS Problem B Badge Problem C Elections Problem D The hat P ...
- Codeforces Round #503 (by SIS, Div. 2)
连接:http://codeforces.com/contest/1020 C.Elections 题型:你们说水题就水题吧...我没有做出来...get到了新的思路,不虚.好像还有用三分做的? KN ...
- Codeforces Round #503 (by SIS, Div. 2) C. Elections (暴力+贪心)
[题目描述] Elections are coming. You know the number of voters and the number of parties — n and m respe ...
- Codeforces Round #503 (by SIS, Div. 2)-C. Elections
枚举每个获胜的可能的票数+按照花费排序 #include<iostream> #include<stdio.h> #include<string.h> #inclu ...
- Codeforces Round #503 (by SIS, Div. 1)E. Raining season
题意:给一棵树每条边有a,b两个值,给你一个m,表示从0到m-1,假设当前为i,那么每条边的权值是a*i+b,求该树任意两点的最大权值 题解:首先我们需要维护出(a,b)的凸壳,对于每个i在上面三分即 ...
- Codeforces Round #503 (by SIS, Div. 2) D. The hat
有图可以直观发现,如果一开始的pair(1,1+n/2)和pair(x, x+n/2)大小关系不同 那么中间必然存在一个答案 简单总结就是大小关系不同,中间就有答案 所以就可以使用二分 #includ ...
- Codeforces Round #503 (by SIS, Div. 2) C. Elections(枚举,暴力)
原文地址 C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分
cf1020D 题意: 交互题目,在有限的询问中找到一个x,使得数列中的第x位和第(x+n/2)位的值大小相同.数列保证相邻的两个差值为1或-1: 思路: 构造函数f(x) = a[x] - a[x ...
- Codeforces Round #503 (by SIS, Div. 2) E. Sergey's problem
E. Sergey's problem [题目描述] 给出一个n个点m条边的有向图,需要找到一个集合使得1.集合中的各点之间无无边相连2.集合外的点到集合内的点的最小距离小于等于2. [算法] 官方题 ...
随机推荐
- JavaScript基本概念A
简介 如果需要了解这些概念, 您应该熟悉 JS 的基本知识. 弱类型 在也无需绞尽脑汁觉得到底采用 float.double,int 还是 short 或是 long 还是 String.只需这样定义 ...
- CIA泄露资料分析(黑客工具&技术)—Windows篇
背景 近期,维基解密曝光了一系列据称来自美国中央情报局(CIA)网络攻击活动的秘密文件,代号为“Vault 7”,被泄露文件的第一部分名为“Year Zero”,共有8761个文件,包含7818个网页 ...
- #调整随机森林的参数(调整n_estimators随机森林中树的数量默认10个树,精度递增显著,但并不是越多越好),加上verbose=True,显示进程使用信息
#调整随机森林的参数(调整n_estimators随机森林中树的数量默认10个树,精度递增显著) from sklearn import datasets X, y = datasets.make_c ...
- UIBezierPath和CAShapeLayer配合肆意画图
一.CAShapeLayer CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画出各种图形 使用CAShapeLayer 绘制一个矩形 let layer ...
- python学习笔记(1)python下载及运行
进入https://www.python.org/官网下载python,根据需要选择2.*或3.*版本 安装完将安装目录添加到环境变量path中 运行cmd,输入python出现版本号即配置成功 下载 ...
- 【转】webService概述
一.序言: 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是We ...
- 爬取google的搜索结果并保存
demo: #coding:utf- import requests from bs4 import BeautifulSoup import bs4 import re def getHTMLTex ...
- HDOJ 1164 Eddy's research
上一篇已经讲了,但是转载别人的很乱,这里自己根据blog里面写的思路,重新写过了一个程序 #include <iostream> #include <malloc.h> #in ...
- Day Day Up—— ——fseek()函数的用法
在牛客网遇到的一个程序题中用到了函数fseek()故查阅了一下该函数的功能及用法,整理如下: fseek函数功能是把文件指针指向文件的开头,需要包含头文件stdio.h 功 能: 重定位流上的文件指针 ...
- ubuntu 12.04 (64位)下安装oracle 11g过程及问题总结
最近公司用到oracle,在ubuntu64位安装了一下,碰到了一些问题,在网上搜索到了一些答案,在此作为笔记记录下来. 1.首先下载oracle并解压不再赘述. 2.安装依赖包 sudo apt-g ...