测试补题6

Problem Statement

You are given a simple undirected graph with �N vertices and �M edges. The vertices are numbered 11 to �N, and the �i-th edge connects vertex ��Ai and vertex ��Bi. Let us delete zero or more edges to remove cycles from the graph. Find the minimum number of edges that must be deleted for this purpose.

What is a simple undirected graph?

What is a cycle?

Constraints

  • 1≤�≤2×1051≤N≤2×105

  • 0≤�≤2×1050≤M≤2×105

  • 1≤��,��≤�1≤Ai,Bi≤N

  • The given graph is simple.

  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NM�1A1​�1B1​�2A2​�2B2​⋮⋮��AM​��BM

Output

Print the answer.

Sample 1

Inputcopy

Outputcopy

6 7

1 2

1 3

2 3

4 2

6 5

4 6

4 5

2

One way to remove cycles from the graph is to delete the two edges between vertex 11 and vertex 22 and between vertex 44 and vertex 55.

There is no way to remove cycles from the graph by deleting one or fewer edges, so you should print 22.

Sample 2

Inputcopy

Outputcopy

4 2

1 2

3 4

0

Sample 3

Inputcopy

Outputcopy

5 3

1 2

1 3

2 3

1

这道题的大概意思就是我们输入的顶点都是连在一起的,然后我们要输出要去掉多少边才可以没有环,这个就是有点像是最小生成树里面的k什么什么算法的一步,就是运用并查集就ok,难是不难,但是!!!翻译真的好难栓q

#include <stdio.h>
int fa[200001];
void chushi(int a)
{
    for(int i=0;i<=a;i++)
    {
        fa[i]=i;
    }
}
int find(int n)
{
    if(fa[n]==n)
    {
        return n;
    }
    else
    {
        fa[n]=find(fa[n]);
        return fa[n];
    }
}
void he(int a,int b)
{
    fa[a]=b;
}

int main()
{
    int n,m;
    int a,b,sum=0,i,j;
    scanf("%d%d",&n,&m);
    chushi(n);
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&a,&b);
        if(find(a)==find(b))
        {
            sum++;
        }
        else
        {
            a=find(a);
            b=find(b);
            he(a,b);
        }
    }
    printf("%d",sum);
}

Given an array �a consisting of �n elements, find the maximum possible sum the array can have after performing the following operation any number of times:

  • Choose 22 adjacent elements and flip both of their signs. In other words choose an index �i such that 1≤�≤�−11≤in−1 and assign ��=−��ai=−ai and ��+1=−��+1ai+1=−ai+1.

Input

The input consists of multiple test cases. The first line contains an integer �t (1≤�≤10001≤t≤1000) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains an integer �n (2≤�≤2⋅1052≤n≤2⋅105) — the length of the array.

The following line contains �n space-separated integers �1,�2,…,��a1,a2,…,an (−109≤��≤109−109≤ai≤109).

It is guaranteed that the sum of �n over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output the maximum possible sum the array can have after performing the described operation any number of times.

Sample 1

Inputcopy

Outputcopy

5

3

-1 -1 -1

5

1 5 -5 0 2

3

1 2 3

6

-1 10 9 8 7 6

2

-1 -1

1

13

6

39

2

Note

For the first test case, by performing the operation on the first two elements, we can change the array from [−1,−1,−1][−1,−1,−1] to [1,1,−1][1,1,−1], and it can be proven this array obtains the maximum possible sum which is 1+1+(−1)=11+1+(−1)=1.

For the second test case, by performing the operation on −5−5 and 00, we change the array from [1,5,−5,0,2][1,5,−5,0,2] to [1,5,−(−5),−0,2]=[1,5,5,0,2][1,5,−(−5),−0,2]=[1,5,5,0,2], which has the maximum sum since all elements are non-negative. So, the answer is 1+5+5+0+2=131+5+5+0+2=13.

For the third test case, the array already contains only positive numbers, so performing operations is unnecessary. The answer is just the sum of the whole array, which is 1+2+3=61+2+3=6.

这道题的意思就是我们有一串数字比如-1 -2 -3 6;然后我们可以移动相邻数字的符号,比如我们移动第一个和第二个,那么就变成了1 2 -3 6;然后我们的目的就是求这段的最大值,可以移动无数次

那么我们就可以发现,

如果他们的负数的数量为偶数,就可以把负数全变成正数,直接相加就ok,如果为奇数,那我们就找出最小的那个,把负号给他,再相加就ok,

还有一种情况是,存在0,有0那就把负号给0,然后还是0,所以就是直接绝对值相加

#include <stdio.h>
#include <math.h>
int main()
{
    int t,n,a[200001];
    scanf("%d",&t);
    while(t--)
    {
        long long teap=0,ans=0,sum=0,min,i;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]==0)//等于0就是全部整数相加
            {
                teap=1;
            }
            if(a[i]<0)
            {
                ans++;//记录有几个负数
            }
        }
        if(teap==1||ans%2==0)
        {
            for(i=1;i<=n;i++)
            {
                a[i]=abs(a[i]);
                sum+=a[i];
            }
            printf("%lld\n",sum);
        }
        else
        {
            min=abs(a[1]);
            for(i=1;i<=n;i++)
            {
                a[i]=abs(a[i]);
                sum+=a[i];
                if(min>a[i]) min=a[i];
            }
            printf("%lld\n",sum-2*min);
        }
    }
}

okok下班下班

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值