题目链接:https://www.luogu.org/problemnew/show/P2881

题目链接:https://vjudge.net/problem/POJ-3275

题目大意

  给定标号为 1~N 这 N 个数,在给定 M 组大小关系,求还需要知道多少组大小关系才可以给这组数排序?

分析1(Floyd + bitset)

  总共需要知道 n * (n - 1) / 2 条边,因此只要求一下现在已经有了多少条边,再减一下即可。由于大小关系有传递性,因此计数之前需要求传递闭包。
  直接上 floyd($O(n^3)​$) 会超时,需要用bitset或手动压位,可以在$O(\frac{n^3}{w})​$求出传递闭包,其中w表示字长,为64或32。

代码如下

 #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)

  考虑到通过压位过的邻接矩阵求传递闭包时做了很多多余的操作,我们可以用邻接链表来求传递闭包,然后用邻接矩阵计数。复杂度可以降到$O(\frac{n * (n + m)}{w})​$。

代码如下

 #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的更多相关文章

  1. USACO翻译:USACO 2014 MARCH GOLD P2 Sabotage

    1.破坏{DOLD题2} sabotage.pas/c/cpp [问题描述] 农夫约翰的头号敌人保罗决定破坏农民约翰的挤奶设备.挤奶设备排成一行,共N(3<= N <=100000)台挤奶 ...

  2. 【POJ3612】【USACO 2007 Nov Gold】 1.Telephone Wire 动态调节

    意甲冠军: 一些树高给出.行一种操作:把某棵树增高h,花费为h*h. 操作完毕后连线,两棵树间花费为高度差*定值c. 求两种花费加和最小值. 题解: 跟NOIP2014 D1T3非常像. 暴力动规是O ...

  3. Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset

    1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 323  Solved ...

  4. USACO 2006 November Gold Corn Fields

    USACO 2006 November Gold Corn Fields 题目描述: Farmer John has purchased a lush new rectangular pasture ...

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

  6. NC25025 [USACO 2007 Nov G]Sunscreen

    NC25025 [USACO 2007 Nov G]Sunscreen 题目 题目描述 To avoid unsightly burns while tanning, each of the \(C\ ...

  7. USACO翻译:USACO 2014 MARCH Silver三题

    USACO 2014 MARCH 一.题目概览 中文题目名称 农田灌溉 懒牛 牛叫 英文题目名称 irrigation lazy mooomoo 可执行文件名 irrigation lazy mooo ...

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

  9. [USACO 2018 Feb Gold] Tutorial

    Link: USACO 2018 Feb Gold 传送门 A: $dp[i][j][k]$表示前$i$个中有$j$个0且末位为$k$的最优解 状态数$O(n^3)$ #include <bit ...

随机推荐

  1. 文件上传 和 base64编码

    base64编码 1.关于Base64编码  :  https://www.cnblogs.com/liyiwen/p/3814968.html (个人猜测),file表单发送文件,肯定是将文件转换为 ...

  2. [ZJOI2011]看电影(组合数学/打表+高精)

    Description 到了难得的假期,小白班上组织大家去看电影.但由于假期里看电影的人太多,很难做到让全班看上同一场电影,最后大家在一个偏僻的小胡同里找到了一家电影院.但这家电影院分配座位的方式很特 ...

  3. Jquery轻量级插件--操作URL

    调用: > "?action=view&section=info&id=123&debug&testy[]=true&testy[]=false ...

  4. CSS:CSS 图像透明/不透明

    ylbtech-CSS:CSS 图像透明/不透明 1.返回顶部 1. CSS 图像透明/不透明 使用CSS很容易创建透明的图像. 注意:CSS Opacity属性是W3C的CSS3建议的一部分. 更多 ...

  5. git clone后切换分支,和远端的不一样。

    原因 git clone后再master分支,切换后到了别的分支,分支里面的文件目录是不一样的,导致出现错误. 解决 删除原来的全部文件 git pull 可是git pull报错, git匹配的文件 ...

  6. 3、获取APP 内存占用率

    关于APP内存占用,不用多说,应该是APP性能测试中比较重要的一点.试想一下,开个应用把手机内存占满了,其它应用无法打开,那么这个应用还会有人安装吗?我觉得是没有的.下面就通过adb命令获取APP虚存 ...

  7. 【C++】清空一个C++栈的快速方法

    来源:https://stackoverflow.com/questions/40201711/how-can-i-clear-a-stack-in-c-efficiently/40201744 传统 ...

  8. Spring Boot Restful WebAPI集成 OAuth2

    系统采用前后端分离的架构,采用OAuth2协议是很自然的事情. 下面开始实战,主要依赖以下两个组件: <dependency> <groupId>org.springframe ...

  9. centos7升级php5.4到php5.6

    history命令历史 8 yum provides php #自带的只有5.4版本 9 rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-relea ...

  10. JDK8新特性之函数式接口

    什么是函数式接口 先来看看传统的创建线程是怎么写的 Thread t1 = new Thread(new Runnable() { @Override public void run() { Syst ...