USACO 2007 “March Gold” Ranking the Cows
题目链接:https://www.luogu.org/problemnew/show/P2881
题目链接:https://vjudge.net/problem/POJ-3275
题目大意
给定标号为 1~N 这 N 个数,在给定 M 组大小关系,求还需要知道多少组大小关系才可以给这组数排序?
分析1(Floyd + bitset)
代码如下
#include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef vector< VI > VVI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, PII > MIPII;
typedef map< string, int > MSI;
typedef multimap< int, int > MMII;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e3 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; int N, M, cnt;
bitset< maxN > m[maxN]; int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
INIT();
N = ri();
M = ri();
For(i, , N) m[i][i] = ;
Rep(i, M) {
int x, y;
x = ri();
y = ri();
m[x][y] = ;
} For(i, , N) For(j, , N) if(m[j][i]) m[j] |= m[i];
For(i, , N) cnt += m[i].count();
cnt -= N; // 减去 i->i 的,有 N 条 printf("%d\n", N * (N - ) / - cnt);
return ;
}
分析2(dfs+ bitset)
代码如下
#include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef vector< VI > VVI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, PII > MIPII;
typedef map< string, int > MSI;
typedef multimap< int, int > MMII;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e3 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; struct Edge{
int from, to;
}; struct Vertex{
VI edges;
}; int N, M, cnt;
bitset< maxN > m[maxN], vis;
Edge e[maxN << ];
int elen;
Vertex v[maxN]; // 找到 x 号节点所能到达的所有节点
void dfs(int x) {
vis[x] = ;
foreach(i, v[x].edges) { // 结果取决于 x 的孩子节点所能到达的节点,此处相当于 m[x][y] = 1
int y = e[*i].to;
if(!vis[y]) dfs(y);
m[x] |= m[y];
}
} int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
INIT();
N = ri();
M = ri();
For(i, , N) m[i][i] = ;
Rep(i, M) {
int x, y;
x = ri();
y = ri();
e[++elen].from = x;
e[elen].to = y;
v[x].edges.PB(elen);
} For(i, , N) if(!vis[i]) dfs(i);
For(i, , N) cnt += m[i].count();
cnt -= N; // 减去 i->i 的,有 N 条 printf("%d\n", N * (N - ) / - cnt);
return ;
}
USACO 2007 “March Gold” Ranking the Cows的更多相关文章
- USACO翻译:USACO 2014 MARCH GOLD P2 Sabotage
1.破坏{DOLD题2} sabotage.pas/c/cpp [问题描述] 农夫约翰的头号敌人保罗决定破坏农民约翰的挤奶设备.挤奶设备排成一行,共N(3<= N <=100000)台挤奶 ...
- 【POJ3612】【USACO 2007 Nov Gold】 1.Telephone Wire 动态调节
意甲冠军: 一些树高给出.行一种操作:把某棵树增高h,花费为h*h. 操作完毕后连线,两棵树间花费为高度差*定值c. 求两种花费加和最小值. 题解: 跟NOIP2014 D1T3非常像. 暴力动规是O ...
- Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset
1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 323 Solved ...
- USACO 2006 November Gold Corn Fields
USACO 2006 November Gold Corn Fields 题目描述: Farmer John has purchased a lush new rectangular pasture ...
- NC25043 [USACO 2007 Jan S]Protecting the Flowers
NC25043 [USACO 2007 Jan S]Protecting the Flowers 题目 题目描述 Farmer John went to cut some wood and left ...
- NC25025 [USACO 2007 Nov G]Sunscreen
NC25025 [USACO 2007 Nov G]Sunscreen 题目 题目描述 To avoid unsightly burns while tanning, each of the \(C\ ...
- USACO翻译:USACO 2014 MARCH Silver三题
USACO 2014 MARCH 一.题目概览 中文题目名称 农田灌溉 懒牛 牛叫 英文题目名称 irrigation lazy mooomoo 可执行文件名 irrigation lazy mooo ...
- poj_3275 Ranking the cows
Ranking the cows Description Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a differe ...
- [USACO 2018 Feb Gold] Tutorial
Link: USACO 2018 Feb Gold 传送门 A: $dp[i][j][k]$表示前$i$个中有$j$个0且末位为$k$的最优解 状态数$O(n^3)$ #include <bit ...
随机推荐
- 排序+模拟+优先队列——cf1248E
其实是个模拟题.. /* 每个人都有一个口渴时间,如果口渴时,其前面没有人在排队,那么其就去排队接水,反之一直等到前面没有人排队 问每个人接完水的时间 每个没轮到的人都在位置上等前面的人接完水,然后他 ...
- django_websocket实现简单聊天室
一.安装模块 pip install channels pip install channels-redis 二.代码 #websocket_v1/settings.py INSTALLED_APPS ...
- NX二次开发-Block UI C++界面Face Collector(面收集器)控件的获取(持续补充 )
Face Collector(面收集器)控件的获取 NX9+VS2012 #include <uf.h> #include <uf_obj.h> UF_initialize() ...
- CSS:CSS 图片廊
ylbtech-CSS:CSS 图片廊 1.返回顶部 1. CSS 图片廊 以下是使用CSS创建图片廊: 图片廊 以下是使用 CSS 创建图片廊: 实例 <div class="res ...
- split("\\s+") 和 split(" +") 有什么区别?
原文地址:https://blog.csdn.net/it_taojingzhan/article/details/51968993 "hello world, this is Al&quo ...
- 2019牛客多校第三场J-LRU management(map+双向链表)
LRU management 题目传送门 解题思路 用map索引对应地址,用双向链表维护序列. 代码如下 #include <bits/stdc++.h> #define INF 0x3f ...
- BZOJ 2460 & 洛谷 P4570 [BJWC2011]元素 (线性基 贪心)
题目链接: 洛谷 BZOJ 题意 给定 \(n\) 个矿石,每个矿石有编号和魔力值两种属性,选择一些矿石,使得魔力值最大且编号的异或和不为 0. 思路 线性基 贪心 根据矿石的魔力值从大到小排序. 线 ...
- Java多态的实现机制是什么,写得非常好!
作者:crane_practice www.cnblogs.com/crane-practice/p/3671074.html Java多态的实现机制是父类或接口定义的引用变量可以指向子类或实现类的实 ...
- js 点击获取验证码后的倒数60s
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script ...
- 将时间 '2018-08-06T10:00:00.000Z' 格式转化为本地时间
参考:https://blog.csdn.net/sxf_123456/article/details/81582964 参考模板: from datetime import datetime, ti ...