热门标签 | 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


推荐阅读
  • MySQL中枚举类型的所有可能值获取方法
    本文介绍了一种在MySQL数据库中查询枚举(ENUM)类型字段所有可能取值的方法,帮助开发者更好地理解和利用这一数据类型。 ... [详细]
  • QUIC协议:快速UDP互联网连接
    QUIC(Quick UDP Internet Connections)是谷歌开发的一种旨在提高网络性能和安全性的传输层协议。它基于UDP,并结合了TLS级别的安全性,提供了更高效、更可靠的互联网通信方式。 ... [详细]
  • 深入理解OAuth认证机制
    本文介绍了OAuth认证协议的核心概念及其工作原理。OAuth是一种开放标准,旨在为第三方应用提供安全的用户资源访问授权,同时确保用户的账户信息(如用户名和密码)不会暴露给第三方。 ... [详细]
  • 2023 ARM嵌入式系统全国技术巡讲旨在分享ARM公司在半导体知识产权(IP)领域的最新进展。作为全球领先的IP提供商,ARM在嵌入式处理器市场占据主导地位,其产品广泛应用于90%以上的嵌入式设备中。此次巡讲将邀请来自ARM、飞思卡尔以及华清远见教育集团的行业专家,共同探讨当前嵌入式系统的前沿技术和应用。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 理解存储器的层次结构有助于程序员优化程序性能,通过合理安排数据在不同层级的存储位置,提升CPU的数据访问速度。本文详细探讨了静态随机访问存储器(SRAM)和动态随机访问存储器(DRAM)的工作原理及其应用场景,并介绍了存储器模块中的数据存取过程及局部性原理。 ... [详细]
  • 几何画板展示电场线与等势面的交互关系
    几何画板是一款功能强大的物理教学软件,具备丰富的绘图和度量工具。它不仅能够模拟物理实验过程,还能通过定量分析揭示物理现象背后的规律,尤其适用于难以在实际实验中展示的内容。本文将介绍如何使用几何画板演示电场线与等势面之间的关系。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 本文介绍如何在应用程序中使用文本输入框创建密码输入框,并通过设置掩码来隐藏用户输入的内容。我们将详细解释代码实现,并提供专业的补充说明。 ... [详细]
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社区 版权所有