热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SPOJOPTMOptimalMarks

OptimalMarksTimeLimit:6000msMemoryLimit:262144KBThisproblemwillbejudgedonSPOJ.OriginalID:O

Optimal Marks

Time Limit: 6000ms
Memory Limit: 262144KB
This problem will be judged on SPOJ. Original ID: OPTM
64-bit integer IO format: %lld      Java class name: Main

You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range [0..231 – 1]. Different vertexes may have the same mark.

For an edge (u, v), we define Cost(u, v) = mark[u] xor mark[v].

Now we know the marks of some certain nodes. You have to determine the marks of other nodes so that the total cost of edges is as small as possible.

Input

The first line of the input data contains integer T (1 ≤ T ≤ 10) - the number of testcases. Then the descriptions of T testcases follow.

First line of each testcase contains 2 integers N and M (0 < N <= 500, 0 <= M <= 3000). N is the number of vertexes and M is the number of edges. Then M lines describing edges follow, each of them contains two integers u, v representing an edge connecting u and v.

Then an integer K, representing the number of nodes whose mark is known. The next K lines contain 2 integers u and p each, meaning that node u has a mark p. It’s guaranteed that nodes won’t duplicate in this part.

Output

For each testcase you should print N lines integer the output. The Kth line contains an integer number representing the mark of node K. If there are several solutions, you have to output the one which minimize the sum of marks. If there are several solutions, just output any of them.

Example

Input:
1
3 2
1 2
2 3
2
1 5
3 100

Output:
5
4
100 
 

Source

Guo HuaYang
 
解题:amber同学的paper上面有的经典最小割题目
技术分享技术分享
  1 #include 
  2 using namespace std;
  3 const int INF = ~0U>>2;
  4 const int maxn = 510;
  5 struct arc{
  6     int to,flow,next;
  7     arc(int x = 0,int y = 0,int z = -1){
  8         to = x;
  9         flow = y;
 10         next = z;
 11     }
 12 }e[maxn*maxn];
 13 int head[maxn],gap[maxn],d[maxn],S,T,tot;
 14 void add(int u,int v,int flow){
 15     e[tot] = arc(v,flow,head[u]);
 16     head[u] = tot++;
 17     e[tot] = arc(u,0,head[v]);
 18     head[v] = tot++;
 19 }
 20 void bfs(){
 21     queue<int>q;
 22     memset(gap,0,sizeof gap);
 23     memset(d,-1,sizeof d);
 24     q.push(T);
 25     d[T] = 0;
 26     while(!q.empty()){
 27         int u = q.front();
 28         q.pop();
 29         ++gap[d[u]];
 30         for(int i = head[u]; ~i; i = e[i].next){
 31             if(e[i^1].flow && d[e[i].to] == -1){
 32                 d[e[i].to] = d[u] + 1;
 33                 q.push(e[i].to);
 34             }
 35         }
 36     }
 37 }
 38 int sap(int u,int low){
 39     if(u == T) return low;
 40     int tmp = 0,a,minH = T - 1;
 41     for(int i = head[u]; ~i; i = e[i].next){
 42         if(e[i].flow){
 43             if(d[u] == d[e[i].to] + 1){
 44                 a = sap(e[i].to,min(low,e[i].flow));
 45                 if(!a) continue;
 46                 e[i].flow -= a;
 47                 e[i^1].flow += a;
 48                 low -= a;
 49                 tmp += a;
 50                 if(!low) break;
 51             }
 52             minH = min(minH,d[e[i].to]);
 53             if(d[S] >= T) return tmp;
 54         }
 55     }
 56     if(!tmp){
 57         if(--gap[d[u]] == 0) d[S] = T;
 58         ++gap[d[u] = minH + 1];
 59     }
 60     return tmp;
 61 }
 62 int maxflow(int ret = 0){
 63     bfs();
 64     while(d[S]  sap(S,INF);
 65     return ret;
 66 }
 67 int n,m,k,mark[maxn],con[maxn];
 68 bool mp[maxn][maxn],vis[maxn];
 69 void build(int x){
 70     S = n + 1;
 71     T = S + 1;
 72     memset(head,-1,sizeof head);
 73     memset(vis,false,sizeof vis);
 74     tot = 0;
 75     for(int i = 0; i i){
 76         if((mark[con[i]]>>x)&1) add(S,con[i],INF);
 77         else add(con[i],T,INF);
 78     }
 79     for(int i = 1; i <= n; ++i)
 80         for(int j = 1; j <= n; ++j)
 81             if(mp[i][j]) add(i,j,1);
 82 }
 83 void dfs(int u,int x){
 84     vis[u] = true;
 85     mark[u] |= (1<<x);
 86     for(int i = head[u]; ~i; i = e[i].next)
 87         if(!vis[e[i].to] && e[i].flow) dfs(e[i].to,x);
 88 }
 89 int main(){
 90     int kase,u,v;
 91     scanf("%d",&kase);
 92     while(kase--){
 93         scanf("%d%d",&n,&m);
 94         memset(mark,0,sizeof mark);
 95         memset(mp,false,sizeof mp);
 96         for(int i = 0; i i){
 97             scanf("%d%d",&u,&v);
 98             mp[u][v] = mp[v][u] = true;
 99         }
100         scanf("%d",&k);
101         for(int i = 0; i i){
102             scanf("%d%d",&u,&v);
103             mark[u] = v;
104             con[i] = u;
105         }
106         for(int i = 0; i <32; ++i){
107             build(i);
108             maxflow();
109             dfs(S,i);
110         }
111         for(int i = 1; i <= n; ++i)
112             printf("%d\n",mark[i]);
113     }
114     return 0;
115 }
View Code

SPOJ OPTM Optimal Marks


推荐阅读
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • 本文介绍了指针的概念以及在函数调用时使用指针作为参数的情况。指针存放的是变量的地址,通过指针可以修改指针所指的变量的值。然而,如果想要修改指针的指向,就需要使用指针的引用。文章还通过一个简单的示例代码解释了指针的引用的使用方法,并思考了在修改指针的指向后,取指针的输出结果。 ... [详细]
author-avatar
羊碧刚_648
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有