当前位置:网站首页>Cow sequencing problem
Cow sequencing problem
2022-06-26 02:23:00 【chengqiuming】
One Original problem description
3275 -- Ranking the Cows
http://poj.org/problem?id=3275
Two Input and output
1 Input
The first 1 Line contains two integers N and M. The first 2 To M+1 That's ok , Each line contains two integers X and Y.X and Y It's all in 1 To N Within the scope of , A cow X Ranking higher than cows Y.
2 Output
At least how many relationships must be investigated for single row output to complete the whole sorting .
3 sample input
5 5
2 1
1 5
2 3
1 4
3 4
4 sample output
3
3、 ... and Algorithm analysis and Design
1 According to the input sample , Create a directed graph

2 According to Transitivity , Got 7 Kind of relationship , Namely
1>4
1>5
2>1
2>3
2>4
2>5
3>4
3 For having n Graph of nodes , The relationship between two people has n(n-1)/2 Kind of ,5 Nodes share 10 Kind of relationship , Also need to know 10-7 = 3 Kind of relationship .
You can use bitset operation , Use one for each node bitset To describe .
bitset<maxn> p[maxn]; // maxn Number of digits ,p[] Represents a binary array
Initialization time ,p[i][i]=1, namely p[i] Of the i Position as 1( Number from the right 0 position , The first 1 position , The first 2 position ).
Input 1-5, Give Way p[1][5] = 1( The first 1 The milk yield of a cow is greater than that of the 5 head ), be p[1]=...100010
Input 1-4, Give Way p[1][4] = 1( The first 1 The milk yield of a cow is greater than that of the 4 head ), be p[1]=...110010
Input 2-1, Give Way p[2][1] = 1( The first 2 The milk yield of a cow is greater than that of the 1 head ), be p[2]=...000110
Input 2-3, Give Way p[2][3] = 1( The first 2 The milk yield of a cow is greater than that of the 3 head ), be p[2]=...001110
Input 3-4, Give Way p[3][4] = 1( The first 3 The milk yield of a cow is greater than that of the 4 head ), be p[3]=...011000
Determine each bit of each array , The following logic exists
if(p[i][j]){
p[i] = p[i]|p[j]; // Bitwise OR operation ,i Cows produce more milk than j The milk yield of a cow , therefore j The milk production relationship between cows and other cows can be passed on to i, So use or relation
}for example :p[2][1]=1, be p[2]=p[2]|p[1]=001110|110010=111110, cow 2 And cows 1 It matters , Then cow 1 To the cows 2.
In this way , You can find the relationship between each point and other points . use ans Accumulate each array element 1 The number of , Because when initializing, you can set yourself to 1, So forget it n Kind of relationship , Need to subtract .
Four Code
package graph;
import java.util.BitSet;
import java.util.Scanner;
public class POJ3275 {
public static void main(String[] args) {
int n, m;
Scanner scanner = new Scanner(System.in);
// n Head ox
n = scanner.nextInt();
// m A relationship
m = scanner.nextInt();
BitSet p[] = new BitSet[n + 1];
for (int i = 1; i <= n; i++) {
p[i] = new BitSet(n + 1);
}
for (int i = 1; i <= n; i++)
p[i].set(i);
while (m-- != 0) {
int u, v;
u = scanner.nextInt();
v = scanner.nextInt();
p[u].set(v);
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
if (p[i].get(k))
p[i].or(p[k]);
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (p[i].get(j)) {
ans++;
}
}
}
System.out.println(n * (n - 1) / 2 - ans + n);
}
}5、 ... and test
Green is the input , White is the output

边栏推荐
猜你喜欢

Tarte aux framboises + AWS IOT Greengrass

基于邻接表的深度优先遍历

Scala Basics (II): variables and data types

Eureka注册信息配置备忘

Digital commodity DGE -- the dark horse of wealth in digital economy

Scala 基础 (二):变量和数据类型

【无标题】vsbiji esp....32

微服务之consul

Implementation of depth first traversal based on adjacency matrix

樹莓派 + AWS IoT Greengrass
随机推荐
微服务之consul
[JS] free API to judge holidays, working days, Saturdays and Sundays
ARM流水线如何提高代码执行效率
Prometeus 2.33.0 新特性
Sqlyog shortcut keys
标定。。。
連接投影儀
[untitled] vsbiji ESP thirty-two
基于邻接矩阵的深度优先遍历实现
heaven and hell's moving
Keda 2.7.1 brief analysis of scaledjob code
Tcp网络通信中各个状态的含义
Connectez Le projecteur
Prompt to update to the latest debug version during vscode debugging
A solution to cross domain problems
基于邻接表的深度优先遍历
How to efficiently complete daily tasks?
连接投影仪
树莓派 + AWS IoT Greengrass
短信插件哪个好用万能表单需要发短信着急测试