1129. Door Painting

博客围绕门染色问题展开,将其转换为出度和入度关系。若每个房子门数为偶数,图是欧拉回路可行;奇数个房子有奇数个门则不行;偶数个房子有奇数个门,可两两匹配形成虚拟走廊构成欧拉回路,最后去掉加入的边。还提及题中有重边,可能不联通有多条欧拉路。

1129. Door Painting

Time limit: 0.25 second Memory limit: 64 MB
There are many rooms, corridors and doors between them in the kindergarten. Some repairs are planned to be made soon. The doors are agreed to be painted in bright cheerful colors: green and yellow. The matron of the kindergarten wants the doors to satisfy the following condition: the sides of an arbitrary door must have the different colors. The number of green doors in each of the lodgings must differ from the number of yellow doors not more than by one. Given the plan of the kindergarten suggest your scheme of door painting.

Input

The first line contains the number of lodgings N ≤ 100 in the kindergarten. The next N lines contain description of the door configuration (k+1-st line contains a description of the k-th lodging). Each of the N lines starts with the number of doors that connect this lodging with adjacent ones. Then there are numbers of adjacent lodgings separated with a space (these numbers follow in ascending order).

Output

should contain a required painting scheme or the word “Impossible” if it is impossible to satisfy the requirements. The colors of the K-th room doors should be put in the K-th line in the same order as they were in the input data. The green color is denoted by G, yellow — by Y.

Sample

inputoutput
5
3 2 3 4
3 1 3 5
4 1 2 4 5
3 1 3 5
3 2 3 4
G Y G
Y G Y
G Y Y G
Y G G
G Y Y
Problem Author: Magaz Asanov Problem Source: VI Ural State University Collegiate Programming Contest (21.10.2001)
***************************************************************************************

将从房子出去的门染红色,进去的门染绿色..也就转换成出度和入度关系;

若每个房子有偶数个门,这整个图就是个欧拉回路,故一定能行

若有奇数个房子有奇数个门,则一定不行(不能构成欧拉路或欧拉回路)

则对于有偶数个房子有奇数个门,则将这偶数个门两两匹配,形成一条虚拟的走廊...则样就形成了欧拉回路了!!最后去掉加入的边即可

这题有重边,可能不联通有多条欧拉路。

******************************************************************************

 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<queue>
 6 #include<stack>
 7 #include<cmath>
 8 using namespace std;
 9 const  int maxn=1001;
10 vector<int>A[maxn][maxn];
11 vector<int>odd;
12 int n,G[maxn][maxn];
13 int mat[maxn][maxn];
14 int vis[maxn];
15 int out[maxn];
16 int i,j,k,a;
17 void dfs(int u)//dfs求欧拉回路或欧拉路
18  {
19      vis[u]=1;
20     for(int it=1;it<=n;it++)
21      {
22          while(G[u][it])
23           {
24               G[u][it]--;
25               G[it][u]--;
26               dfs(it);
27               A[u][it].push_back(1);
28               A[it][u].push_back(2);
29           }
30      }
31  }
32  int main()
33  {
34      memset(out,0,sizeof(out));
35      memset(G,0,sizeof(G));
36      odd.clear();
37      cin>>n;
38      for(i=1;i<=n;i++)
39       {
40           cin>>out[i];
41           if(out[i]%2)
42            odd.push_back(i);
43          for(j=1;j<=out[i];j++)
44           {
45               cin>>a;
46               G[i][a]++;
47           }
48       }
49       memcpy(mat,G,sizeof(G));
50     if(odd.size()%2)//度为奇数的点的个数为奇数时情况,不存在
51      {
52          cout<<"Impossible"<<endl;
53          return 0;
54      }
55     for(i=0;i<odd.size();i+=2)
56      {
57          int u=odd[i];//出度为奇点的边度两两加1
58          int v=odd[i+1];
59          G[u][v]++;
60          G[v][u]++;
61      }
62      memset(vis,0,sizeof(vis));
63      memset(A,0,sizeof(A));
64      for(i=1;i<=n;i++)
65       if(!vis[i])
66        dfs(i);
67      for(i=1;i<=n;i++)
68       {
69           bool gs=true;
70           for(j=1;j<=n;j++)
71            {
72                int k=0;
73                while(mat[i][j])
74                 {
75                     mat[i][j]--;
76                     if(!gs)
77                       cout<<' ';
78                     else
79                       gs=false;
80                      if(A[i][j][k]==1)//输出记录的路径
81                       cout<<'G';
82                      else
83                        cout<<'Y';
84                 }
85            }
86           cout<<endl;
87       }
88     return 0;
89 
90  }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3293539.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值