The Necklace UVA10054

本文介绍了一个关于复原散落项链的编程问题,包括输入格式、输出要求及解决方案,采用Fleury算法来判断是否能形成环,并输出正确的珠子排列顺序。实例演示了如何通过代码实现这一过程。



Problem D: The Necklace

My little sister had a beautiful necklace made of colorful beads. Two successive beads in the necklace shared a common color at their meeting point. The figure below shows a segment of the necklace:

But, alas! One day, the necklace was torn and the beads were all scattered over the floor. My sister did her best to recollect all the beads from the floor, but she is not sure whether she was able to collect all of them. Now, she has come to me for help. She wants to know whether it is possible to make a necklace using all the beads she has in the same way her original necklace was made and if so in which order the bids must be put.

Please help me write a program to solve the problem.

Input

The input containsTtest cases. The first line of the input contains the integerT.

The first line of each test case contains an integerN($5 \leN \le 1000$) giving the number of beads my sister was able to collect. Each of the nextNlines contains two integers describing the colors of a bead. Colors are represented by integers ranging from 1 to 50.

Output

For each test case in the input first output the test case number as shown in the sample output. Then if you apprehend that some beads may be lost just print the sentence ``some beads may be lost" on a line by itself. Otherwise, printNlines with a single bead description on each line. Each bead description consists of two integers giving the colors of its two ends. For$1 \le i \le N ­ 1$, the second integer on lineimust be the same as the first integer on linei+ 1. Additionally, the second integer on lineNmust be equal to the first integer on line 1. Since there are many solutions, any one of them is acceptable.

Print a blank line between two successive test cases.

Sample Input

2
5
1 2
2 3
3 4
4 5
5 6
5
2 1
2 2
3 4
3 1
2 4

Sample Output

Case #1
some beads may be lost
 
Case #2
2 1
1 3
3 4
4 2
2 2



Miguel Revilla
2000-12-28

又是一道无向图的欧拉图问题,首先判定是不是欧拉图,再输出,这里用到了一个算法:

Fleury算法:
    (1)任取v0∈V(G),令P0=v0;
    (2)设Pi=v0e1v1e2...eivi已经行遍,按下面方法来从E(G)-{e1,e2,...,ei}中选
    取ei+1:
    (a)ei+1与vi想关联;
    (b)除非无别的边可供行遍,否则ei+1不应该为Gi=G-{e1,e2,...,ei}中的桥.
    (3)当(2)不能再进行时,算法停止。
例如数据:用电脑把每一步输出,大致理解就是随便找个点然后一它为起点开始遍历,到不能继续时,返回上一层,这是侯输出刚才无法继续遍历的那个店,也就是以他为起点,利用dfs的递归返回时的过程输出,这个算法比回溯快些。
#include<iostream>
#include<cstring>

using namespace std;

int grid[100][100];
int du[100];
int m,tag;

void euler(int pos)
{
    for(int i=1;i<=50;i++)
    {
        if(grid[pos][i]>0)
        {
            grid[pos][i]--;
            grid[i][pos]--;
            euler(i);
            cout<<i<<" "<<pos<<endl;
        }
    }
}

int main()
{
    int n,t;
    cin>>n;
    for(t=1;t<=n;t++)
    {
        memset(grid,0,sizeof(grid));
        memset(du,0,sizeof(du));
        cin>>m;
        int i,j,k;
        for(i=0;i<m;i++)
        {
            cin>>j>>k;
            grid[j][k]++;
            grid[k][j]++;
            du[j]++;
            du[k]++;
        }
        tag=1;
        for(i=1;i<=50;i++)
            if(du[i]%2!=0)
                tag=0;
        cout<<"Case #"<<t<<endl;
        if(tag==0) cout<<"some beads may be lost"<<endl;
        else euler(k);
        if(t<n) cout<<endl;
    }
    return 0;
}

### 2021 CCPC Guangzhou Onsite 题目解析 #### H题: Three Integers 该题目主要考察的是分类讨论以及合理运用倍数关系的能力。当输入数据中存在一个零时,需特别注意是否存在数值为1的情况。在这种情况下,为了防止被特定测试用例攻击(即所谓的“hack”),应适当调整倍数逻辑[^1]。 以下是实现此算法的一个可能代码示例: ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; vector<int> nums={abs(a), abs(b), abs(c)}; sort(nums.begin(),nums.end()); long long res=LLONG_MAX; do{ if(nums[1]==0 && nums[2]!=0){ // 特殊情况处理 res=min(res,(long long)(nums[0]*nums[2])); } else { // 正常计算最小操作次数 long long temp=(nums[1]-nums[0]%nums[1])%nums[1]; temp += (nums[2]-nums[1]%nums[2])%nums[2]; res = min((long long)res,temp); } }while(next_permutation(nums.begin(),nums.end())); cout<<res<<"\n"; } ``` #### C题: Necklace 对于C题Necklace而言,其核心思路在于如何通过贪心策略来解决问题。具体来说,可以从第一个节点出发尝试尽可能地向右扩展路径长度;一旦遇到超出允许的最大长度`max_len`的情形,则将整个项链结构向左侧平移一定距离,从而最大化利用可用空间并减少资源浪费[^4]。 下面给出了一种基于上述思想的解决方案框架: ```cpp // 假设已知一些变量定义如 max_len, nodes 等... bool check(double mid){ double sum=0; for(auto &len:nodes){ if(sum+len<=mid){ sum+=len; } else{ return false; } } return true; } double binarySearch(){ double l=0,r=accumulate(nodes.begin(),nodes.end(),0.0); while(r-l>=EPS){ double mid=l+(r-l)/2; if(check(mid)){ r=mid-EPS; } else{ l=mid+EPS; } } return l; } ``` 以上两道典型题目展示了不同类型的解法技巧——前者依赖于细致入微的状态枚举与边界条件判断,后者则体现了经典优化理论中的动态规划与二分查找相结合的应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值