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. [算法] 官方题 ...
随机推荐
- PowerDesigner中批量替换name和code的脚本
无论是cdm还是pdm都可以批量替换.处理.可在Tool-Execute commands-Edit/Run script中编辑运行脚本: 下面的脚本是批量将CDM中实体的用Code替换掉Name O ...
- Eloquent ORM模型中添加自定义值
我们都知道通过Laravel中数据库查询出来的模型对象都是基于数据库字段,今天给大家展示一个 Laravel Eloquent ORM 模型特性-附加值不存在于数据表中. 举个简单的栗子,一篇文章(p ...
- 关于使用字库-雅黑字体(msyh.ttf )显示中文的一些。。。
开发中有关程序在使用字库 雅黑字体的 的时候 vs下一开始没有显示出中文来,都是乱码. 在android下使用该字体库的时候同样也没有显示出中文,后来搜搜了原因,得知编码必须是UTF-8 也就是使用字 ...
- 【262】pscp命令 实现windows与linux互传文件
首先将pscp.exe文件放在某个文件夹中 新建*.bat文件 w-wx.bat代码 @echo off pscp.exe -pw l*****h D:\Windows-Linux\Data\* oc ...
- C++——virtual
一.放在父类的函数名前面:多态 1.作用:实现多态:子类可以自定义父类中的virtual函数 #include <iostream> using namespace std; class ...
- noi.ac day6t3 color
传送门 分析 将询问离线,枚举右端点.新加入一个右端点i时,假设离i第t近的同色位置为p,t+1近的是q,则当i是右端点时,(q,p]的点可以作为左端点. 注意对于一个点离它第t近的同色点可以用队列维 ...
- ZROI2018提高day5t3
传送门 分析我们可以根据性质将这个序列构造成一个环:0,a[1~n],0,a[n~1] 这中间的0是为了起间隔作用的. 我们又知道b[i]=a[i-1]^a[i+1] c[i]=b[i-1]^b[i+ ...
- 之前在不网站看到过关于css的一些例子 今天自己也写了一个css特效
下面是代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- Entity Framework Tutorial Basics(13):Database First
Database First development with Entity Framework: We have seen this approach in Create Entity Data M ...
- HtmlAgilityPack HTML操作类库的使用
HtmlAgilityPack是.NET下的一个开源的HTML解析类库.支持用XPath来解析HTML.命名空间:HtmlAgilityPack. 1.读取网络中html网页内容,获取网页中元素bod ...