当前位置:   article > 正文

cf1038E(暴力DP/bfs)

cf1038E(暴力DP/bfs)

一个块可以看做是无向图上的边,然后就变成了在无向图上跑欧拉路径。。

4个点应该是可以随便暴力了。。不过边比较多。。

如果考虑哪些边不走,能注意到2条重边可以构成一个简单环。。所以如果不走肯定是亏的。。

所以对重边来说,最多只能不经过一条边。。而本质不同的边其实也就8条。。拆出来就变成16条。。

然后暴力bfs/DP或者直接爆搜应该就可以了。。。

 

 

 

  1. #include<bits/stdc++.h>
  2. #define inc(i,l,r) for(int i=l;i<=r;i++)
  3. #define dec(i,l,r) for(int i=l;i>=r;i--)
  4. #define link(x) for(edge*j=h[x];j;j=j->next)
  5. #define mem(a) memset(a,0,sizeof(a))
  6. #define succ(x) (1<<(x))
  7. #define lowbit(x) (x&(-x))
  8. #define ll long long
  9. #define eps 1e-8
  10. #define mid (x+y>>1)
  11. #define sqr(x) ((x)*(x))
  12. #define NM 1005
  13. #define nm 200005
  14. const double pi=acos(-1);
  15. const int inf=1e9+7;
  16. using namespace std;
  17. ll read(){
  18. ll x=0,f=1;char ch=getchar();
  19. while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
  20. while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
  21. return x*f;
  22. }
  23. ll n,ans;
  24. int m,prime[NM],tot,mu[NM];
  25. bool v[NM];
  26. void init(){
  27. m=100;mu[1]=1;
  28. inc(i,2,m){
  29. if(!v[i])prime[++tot]=i,mu[i]=-1;
  30. inc(j,1,tot){
  31. if(i*prime[j]>m)break;
  32. v[i*prime[j]]++;
  33. if(i%prime[j]==0)break;
  34. mu[i*prime[j]]=-mu[i];
  35. }
  36. }
  37. }
  38. ll solve(int t){
  39. ll ans=0;
  40. ans=(ll)pow(n,1.0/t-eps);
  41. while(1){
  42. ll tmp=1;
  43. bool f=true;
  44. inc(j,1,t){
  45. if(n/(ans+1)<tmp){f=false;break;}
  46. tmp*=ans+1;
  47. if(tmp>n){f=false;break;}
  48. }
  49. if(!f)break;else ans++;
  50. }
  51. return ans;
  52. }
  53. int main(){
  54. init();
  55. int _=read();while(_--){
  56. n=read();ans=0;
  57. inc(i,2,m){
  58. ll t=solve(i);
  59. if(t==1)break;
  60. ans+=mu[i]*(t-1);
  61. }
  62. printf("%lld\n",n+ans-1);
  63. }
  64. return 0;
  65. }

 

 

 

 

E. Maximum Matching

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given n

blocks, each of them is of the form [color1|value|color2], where the block can also be flipped to get [color2|value|color1

].

A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C.

The value of the sequence is defined as the sum of the values of the blocks in this sequence.

Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence.

Input

The first line of input contains a single integer n

(1≤n≤100

) — the number of given blocks.

Each of the following n

lines describes corresponding block and consists of color1,i, valuei and color2,i (1≤color1,i,color2,i≤4, 1≤valuei≤100000

).

Output

Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence.

Examples

Input

Copy

6
2 1 4
1 2 4
3 4 4
2 8 3
3 16 3
1 32 2

Output

Copy

63

Input

Copy

7
1 100000 1
1 100000 2
1 100000 2
4 50000 3
3 50000 4
4 50000 4
3 50000 3

Output

Copy

300000

Input

Copy

4
1 1000 1
2 500 2
3 250 3
4 125 4

Output

Copy

1000

Note

In the first example, it is possible to form a valid sequence from all blocks.

One of the valid sequences is the following:

[4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2]

The first block from the input ([2|1|4] →

[4|1|2]) and second ([1|2|4] →

[4|2|1]) are flipped.

In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped):

[2|100000|1] [1|100000|1] [1|100000|2]

In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号