题目大意:每个同学可以指定一个人,然后构成一个有向图。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 (拓扑)的更多相关文章

  1. 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 ...

  2. Codeforces Round #503 (by SIS, Div. 2)

    连接:http://codeforces.com/contest/1020 C.Elections 题型:你们说水题就水题吧...我没有做出来...get到了新的思路,不虚.好像还有用三分做的? KN ...

  3. 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 ...

  4. Codeforces Round #503 (by SIS, Div. 2)-C. Elections

    枚举每个获胜的可能的票数+按照花费排序 #include<iostream> #include<stdio.h> #include<string.h> #inclu ...

  5. Codeforces Round #503 (by SIS, Div. 1)E. Raining season

    题意:给一棵树每条边有a,b两个值,给你一个m,表示从0到m-1,假设当前为i,那么每条边的权值是a*i+b,求该树任意两点的最大权值 题解:首先我们需要维护出(a,b)的凸壳,对于每个i在上面三分即 ...

  6. Codeforces Round #503 (by SIS, Div. 2) D. The hat

    有图可以直观发现,如果一开始的pair(1,1+n/2)和pair(x, x+n/2)大小关系不同 那么中间必然存在一个答案 简单总结就是大小关系不同,中间就有答案 所以就可以使用二分 #includ ...

  7. 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 ...

  8. Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分

    cf1020D 题意: 交互题目,在有限的询问中找到一个x,使得数列中的第x位和第(x+n/2)位的值大小相同.数列保证相邻的两个差值为1或-1: 思路: 构造函数f(x) = a[x] - a[x ...

  9. Codeforces Round #503 (by SIS, Div. 2) E. Sergey's problem

    E. Sergey's problem [题目描述] 给出一个n个点m条边的有向图,需要找到一个集合使得1.集合中的各点之间无无边相连2.集合外的点到集合内的点的最小距离小于等于2. [算法] 官方题 ...

随机推荐

  1. [机器学习基础]矩阵基础和numpy

    矩阵定义:[摘自百度百科] 由 m × n 个数aij排成的m行n列的数表称为m行n列的矩阵,简称m × n矩阵.记作: 这m×n 个数称为矩阵A的元素,简称为元,数aij位于矩阵A的第i行第j列,称 ...

  2. [Chapter 3 Process]Practice 3.1 相关知识:进程创建、fork函数

    3.1 Using the program shown in the Figure3.30, explain what the output will be at LINE A 答案:LINE A 处 ...

  3. svn 撤销修改

    1,更新到最新版本 不知道是哪个文件的问题时,可以svn log查看日志,基本上可以通过哪一次提交修改了哪一个文件推算出 是哪个文件的问题. 若知道是哪个文件的问题,直接svn log 文件名 大致确 ...

  4. Python之POST登录测试

    不解释,直接上代码: #!/usr/bin/env python # -*- encoding: utf-8 -*- """ @version: v1.0 @author ...

  5. from xml.etree import cElementTree as ET

  6. centos7部署func

    Func(Fedora Unitied Network Controller)是红帽公司以Fedora平台构建的统一网络控制器,是为解决集群管理.监控问题而设计开发的系统管理基础框架.它是一个能有效简 ...

  7. day17-jdbc 7.Statement介绍

    SQL语句:DML.DQL.DCL.DDL.DML和DQL是用的最多的.DCL和DDL用的很少. 程序员一般是操作记录,创建一表很少. package cn.itcast.jdbc; import c ...

  8. sg值的求解(NIM)

    硬币游戏2 挑战程序设计竞赛P315 1堆的情况: #include<bits/stdc++.h> ,grundy[],k=,A[]={,},n=; using namespace std ...

  9. Codeforces 1142B Lynyrd Skynyrd

    ---恢复内容开始--- 题意:给你一个排列p和数组a,有t组询问,每次询问一个区间的子序列中是否有p的一个循环排列. 思路:以p = [3, 1, 2]举例, 我们扫描数组b,假设当前数字是1,那么 ...

  10. [转]怎么学习前端,尤其是 JavaScript 这块

    1. 先看看 w3school ,了解什么是 js,再找几本写 js 小效果的书看看,知道 js 干什么: 2. 然后再去通读 API,推荐 <Javascript权威指南>,第四版吧,第 ...