【HDU - 4781】Assignment For Princess(图上构造)

题干:

  Long long ago, in the Kingdom Far Far Away, there lived many little animals. And you are the beloved princess who is marrying the prince of a rich neighboring kingdom. The prince, who turns out to be a handsome guy, offered you a golden engagement ring that can run computer programs! 
  The wedding will be held next summer because your father, the king, wants you to finish your university first. 
  But you did’t even have a clue on your graduation project. Your terrible project was to construct a map for your kingdom. Your mother, the queen, wanted to make sure that you could graduate in time. 
  Or your wedding would have to be delayed to the next winter. So she told you how your ancestors built the kingdom which is called the Roads Principle: 

  1. Your kingdom consists of N castles and M directed roads. 
  2. There is at most one road between a pair of castles. 
  3. There won’t be any roads that start at one castle and lead to the same one. 
  She hoped those may be helpful to your project. Then you asked your cousin Coach Pang (Yes, he is your troubling cousin, he always asks you to solve all kinds of problems even you are a princess.), the Minister of Traffic, about the castles and roads. Your cousin, sadly, doesn’t have a map of the kingdom. Though he said the technology isn’t well developed and it depends on your generation to contribute to the map, he told you the Travelers Guide, the way travelers describe the amazing road system: 
  1. No matter which castle you start with, you can arrive at any other castles. 
  2. Traveling on theM roads will take 1, 2, 3, ... ,M days respectively, no two roads need the same number of days. 
  3. You can take a round trip starting at any castle, visiting a sequence of castles, perhaps visiting some castles or traveling on some roads more than once, and finish your journey where you started.
  4. The total amount of days spent on any round trip will be a multiple of three. 
  But after a month, you still couldn’t make any progress. So your brother, the future king, asked your university to assign you a simpler project. And here comes the new requirements. Construct a map that satisfies both the Roads Principle and the Travelers Guide when N and M is given. 
  There would probably be several solutions, but your project would be accepted as long as it meets the two requirements. 
Now the task is much easier, furthermore your fiance sent two assistants to help you. 
  Perhaps they could finish it within 5 hours and you can think of your sweet wedding now.

Input

  The first line contains only one integer T, which indicates the number of test cases. 
  For each test case, there is one line containing two integers N, M described above.(10 <= N <= 80, N+3 <= M <= N 2/7 )

Output

  For each test case, first output a line “Case #x:”, where x is the case number (starting from 1). 
  Then output M lines for each test case. Each line contains three integers A, B, C separated by single space, which denotes a road from castle A to castle B and the road takes C days traveling. 
  Oh, one more thing about your project, remember to tell your mighty assistants that if they are certain that no map meets the requirements, print one line containing one integer -1 instead. 
  Note that you should not print any trailing spaces.

Sample Input

1
6 8

Sample Output

Case #1:
1 2 1
2 3 2
2 4 3
3 4 4
4 5 5
5 6 7
5 1 6
6 1 8  

Hint

The restrictions like N >= 10 will be too big for a sample. So the sample is just a simple case for the detailed formats of input and output,and it may be helpful for a better understanding. Anyway it won’t appear in actual test cases.

题目大意:

现在给你n个点 m条边,你要构造出来一个有向图,有向图的边权各不相同,从1到m 。并且要求从一个点到其他所有点都是联通的,还要求从一个点出发,回到该点走的路径权值一定要是3的倍数,并且任意两点之间最多只能有一条边,

解题报告:

因为题目保证有解,所以直接连接所有顶点成环,成链的边的权值直接就是1~n-1就行,因为n号点到1号点的边一定可以通过其他权值构造出来,因为这题保证了N+3 <= M。构造一个环了之后看还剩下多少权值,在模3相等的点上直接加边就行了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e4 + 5;
bool ok[MAX];
int sum[MAX];
int n,m,top[MAX],qq[3][MAX];
int main()
{int t,iCase=0;cin>>t;while(t--) {scanf("%d%d",&n,&m);memset(sum,0,sizeof sum);printf("Case #%d:\n",++iCase);for(int i = 1; i<=n-1; i++) {printf("%d %d %d\n",i,i+1,i);sum[i+1] = sum[i] + i;}top[0]=top[1]=top[2]=0;for(int val = n; val<=m; val++) qq[val%3][++top[val%3]] = val;int x = (30000000 + (sum[1]-sum[n])%3)%3;printf("%d 1 %d\n",n,qq[x][top[x]--]);for(int i = 1; i<=n-2; i++) {for(int j = i+2; j<=n; j++) {if(i!=1 || j!=n) {int x = (30000000 + sum[j] - sum[i])%3;if(top[x]) printf("%d %d %d\n",i,j,qq[x][top[x]--]);}}}}return 0 ;
}
/*
1
4 7*/

 

总结:

注意前缀和算的时候不能这样写:

for(int i = 1; i<n-1; i++) {printf("%d %d %d\n",i,i+1,i);sum[i] = sum[i-1] + i;ok[i]=1;sum += i;}

另一点:这两句是等价的。当不好估计上界的时候就可以下面这样写。

int x = (30000000 + (sum[1]-sum[n])%3)%3;
int x = (3+(sum[1]-sum[n])%3)%3;

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440196.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vim编辑器最常用的快捷键

Vim编辑器常用快捷键 光标移动到行首、行尾&#xff1a; 数字0/$ 光标移动到第一行、最后一行&#xff1a; 大写H/L 快速移动到第一行也可以用gg 光标移动到第几行、从当前位置跳跃几行&#xff1a; 行号大写G 跳…

脑残式网络编程入门(一):跟着动画来学TCP三次握手和四次挥手

转自即时通讯网&#xff1a;http://www.52im.net/ 1、引言 网络编程中TCP协议的三次握手和四次挥手的问题&#xff0c;在面试中是最为常见的知识点之一。很多读者都知道“三次”和“四次”&#xff0c;但是如果问深入一点&#xff0c;他们往往都无法作出准确回答。 本篇文章尝…

【HDU - 5452】Minimum Cut(树形dp 或 最近公共祖先lca+树上差分,转化tricks,思维)

题干&#xff1a; Given a simple unweighted graph GG (an undirected graph containing no loops nor multiple edges) with nn nodes and mm edges. Let TT be a spanning tree of GG. We say that a cut in GG respects TT if it cuts just one edges of TT. Since love…

Idea自带的工具打jar包和Maven打Jar包(SpringBoot工程)

1.Idea自带的工具打jar包 &#xff08;1&#xff09;点击菜单栏的File后选中Project Structure&#xff0c;接着按如下图所示操作&#xff1a; &#xff08;2&#xff09;点击“OK”按钮后会出现下图的界面&#xff0c;然后继续点击“OK”按钮 &#xff08;3&#xff09;现在开…

图解算法学习笔记(目录)

今天遇到一本好书&#xff0c;如下&#xff0c;书很薄&#xff0c;不到200页&#xff0c;有将近400张图片&#xff0c;算法介绍的很有趣。这也是我读的第三本袁国忠先生翻译的书&#xff0c;向两位致敬。 目录大致如下; 第1章&#xff1a;二分查找和大O表示法&#xff1b; 第…

2019ACM浪潮杯山东省赛参赛总结

emmm是要记录一下生活了呢&#xff0c;不然以后退役了连自己经历过什么都记不住了。 5.11周六&#xff0c;早上5:30分&#xff0c;qdu集训队一行40人(左右)集合登上大巴&#xff0c;前往济南大学参加ACM省赛。上车清点了一下人数&#xff0c;然后发车以后就睡着了&#xff0c;…

Linux系统查看开放的端口、开启指定端口、关闭指定端口和查看及删除定时任务

Linux系统管理端口的操作命令 以下操作在需要开启防火墙&#xff0c;防火墙的开启(重启)、关闭和查看防火墙的状态见末尾 1.查看所有已经对外开放的端口&#xff1a;firewall-cmd --list-ports 2.开启指定的端口&#xff1a;firewall-cmd --zonepublic --add-port8080/tcp -…

图解算法学习笔记(一): 算法简介

本章内容&#xff1a; 编写第一种查找算法——二分查找。 学习如何谈论算法的运行时间——大O表示法。 1) 算法是一组完成任务的指令&#xff0c;任何代码片段都可视为算法。 2)二分查找&#xff1a;一种查找算法&#xff0c;其输入是一个有序的元素列表。 Python实现二分查…

用友通ERP客户端报无法登陆错

用友通ERP客户端报“无法登陆”错 排除系统版本错&#xff0c;要求windows xp professional sp2&#xff1b;client.dll文件错误后 请确认 c:/windows/system32/drivers/etc/目录下存在 hosts 文件&#xff0c;并且含有 127.0.0.1 localhost 行

【POJ - 3249】Test for Job(DAG线性求带负权的最长路,dp)

题干&#xff1a; Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, Its hard to have a job, since there are swelling numbers of the unemployed. So some companies often use hard tests for …

图解算法学习笔记(二): 选择排序

目录 1)数组和链表&#xff1a; 2)选择排序算法&#xff1a; 3)小结 本章内容&#xff1a; 两种基本数据结构&#xff1a;数组和链表&#xff1b; 选择排序算法&#xff1b; 1)数组和链表&#xff1a; 数组是连续的内存单元&#xff0c;链表可以不连续&#xff1b; 链表…

javascript递归遍历文件夹下面的所有文件并返回所有文件全路径名称数组以及解析JavaScript方法体字符串的结束位置

一、前端脚本经常需要用到遍历指定文件夹下面的所有文件&#xff08;包含子文件夹&#xff09;的内容并做特定的逻辑处理&#xff0c;下面给出同步遍历的方式&#xff0c;开箱即用。 const fs require(fs);main()function main() {let allFiles getAllFiles(srcDir);console…

WinXP下替代IIS的新思路

WinXP下&#xff0c;.Net服务器有两个选择&#xff0c;IIS和Webdev.webservice。然而&#xff0c;IIS有最多十连接的限制&#xff0c;网上的解决方式&#xff08;包括注册表修改、微软工具、NTSwitch&#xff09;均未能突破&#xff1b;webdev.webservice虽没有连接数量限制&am…

【HDU - 5456】Matches Puzzle Game(数位dp,思维)

题干&#xff1a; As an exciting puzzle game for kids and girlfriends, the Matches Puzzle Game asks the player to find the number of possible equations A−BCA−BC with exactly n (5≤n≤500)n (5≤n≤500) matches (or sticks). In these equations, A,BA,B and …

图解算法学习笔记(三):递归

本章内容&#xff1a; 学习递归&#xff1b;如何将问题分解成基线条件和递归条件。 1) 每个递归函数都有两部分&#xff1a;基线条件(base case)和递归条件(recursive base)。例如&#xff1a;打印3...2...1 def countdown(i):print(i)if i < 0:returnelse:countdown(i…

深入理解Angular模块化概念

深入理解Angular模块NgModule装饰器 Angular应用程序全部是由 模块化组成的 &#xff0c;即模块化开发&#xff08;组件/指令/服务/管道/路由&#xff09;&#xff0c;与js模块化是不同的概念&#xff0c;但具有异曲同工之妙&#xff1b; Angular 模块之间是隔离 的&#xff0…

在IIS中启用父路径,不被黑客利用

在IIS中&#xff0c;有时要启用父路径&#xff0c;但黑客常常利用父路径访问硬盘文件。因此&#xff0c;我使用了一种方法&#xff1a; 先将IIS暂停&#xff0c;启用父路径。之所以暂停是为了防止操作时遭攻击然后&#xff0c;在根目录下创建虚拟路径“..”&#xff0c;任意选择…

【HRBUST - 1996】数学等式 (HASH 或 二分)

题干&#xff1a; 又到了数学题的时刻了&#xff0c;给出三个数组A,B,C,然后再给出一个数X&#xff0c;现在我想知道是否能找到三个数满足等式A[i]B[j]C[k]X&#xff0c;你能帮助我么&#xff1f;&#xff1f; Input 本题有多组数据&#xff0c;每组数据第一行输入三个数n, …

Apollo自动驾驶入门课程第⑨讲 — 控制(上)

目录 1. 简介 2. 控制流程 3. PID控制 4. PID优劣对比 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 9月26日 上周我们发布了无人驾驶技术的 规划篇&#xff0c;车辆基于高精地图&#xff0c;感知和预测模块的数据来进行这一…

Angular高版本中为自定义的独立class类添加显式的Angular装饰器

Angular9或10及以后的版本&#xff0c;如果自定义的类上面没有写装饰器的话&#xff0c;编译后在Browser平台不会报错&#xff0c;但是在执行打包命令npm run build --prod时就会报错如下所示&#xff1a; error NG2007: Class is using Angular features but is not decorate…