Sie sind auf Seite 1von 14

code chef problems

Receipt Recovery
ALL SUBMISSIONS
All submissions for this problem are available.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
While vacationing in Egypt, Chef found a recipe of an ancient dish. The receipt is a sequence of hieroglyphs
written on a magic paper - it survived for thousands of years! The sequence is written on a long scroll.
Unfortunately, the scroll is split into pieces, and possibly, some pieces of the scroll are missing. Chef has a
total of N pieces, numbered for convenience from 1 to N. He gave the pieces of a tape to his friend who
studies hieroglyphs for analysis. Chef was happy to hear that there are some relations between the pieces.
Some of the pieces may follow other pieces.
The informations about these relations is given as a list of M pairs (Ai, Bi). Such a pair means that the Aith
piece can be immediately followed by the Bith one.
Chef is working on recovering the recipe to prepare this dish for his Egyptian friends. He wants to place all
the pieces into lines such that any pair of consecutive pieces (L, R) in a line must be explicitly allowed by
one of the M rules described above. In other words, for some i (1 i M), (Ai, Bi) = (L, R). It appears that
in general, not all the pieces can be placed in a single line while following the rules. So, please help Chef
find what is the minimum number of lines required for all the pieces to be placed acoording to the rules.
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test
cases follows.
The first line of each test case contains two space-separated integers N and M denoting the number of
pieces and number of relations of those N pieces. Next M lines contain two space-separated integers A and
B denoting that piece number A can be followed by piece number B, all those pairs are guaraneed to be
unique within a test case.
Output
For each test case, output a single line containing the minimum number of lines required for all the pieces
to be placed acoording to the rules.
Constraints
1 T 20
1 N 100
1 M N2
1 Ai N
1 Bi N
There is no sequence S1, S2, ..., Sk such that the piece Si can be immediately followed by the piece Si+1
for all i from 1 to k-1, and that piece Sk can be followed by piece S1.
Example
Input:
3

33
12
23
13
32
12
13
54
13
23
34
25
Output:
1
2
2
sloution to recipt rocovery
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef enum { false, true } bool
#define MAXN 102
int graph[MAXN][MAXN], T, N, M, i, j, k;
int same[MAXN];
bool visited[MAXN];
// Depth first search
bool dfs(int no){
if(no == -1){return 1;}
if(visited[no]==true){return false;}
visited[no] = true;
int l = 0;

for (l = 0; l < N; l++){


if (graph[no][l] > 0 && no != same[l] && dfs(same[l])){
same[l] = no;
return true;
}
}
return false;
}
int main(void){
// Gets T cases
scanf("%d", &T);
// Loop through T cases
while(T--){
// Gets M and N
scanf("%d %d", &N, &M);

// Initialize our 'graph'


for (j = 0; j < MAXN; j++){
for (k = 0; k < MAXN; k++){
graph[j][k] = 0;
}
}
// Gets sequence of characters
for (j = 0; j < M; j++){
int x, y;
scanf("%d %d", &x, &y);
graph[x-1][y-1] = 1;
}
// Initialize visisted array
memset(same, -1, sizeof(same));
// min sequence = N - max traverse

int max_travs = 0;
for (k = 0; k < N; k++){
// initialize visited bool
memset(visited, false, sizeof(visited));
if(dfs(k)){
max_travs++;
}
}
printf("%d\n", (N-max_travs));
}
return 0;
}
second problem...........................................
Chef has a collection of N postage stamps. Each stamp belongs to some type, which are enumerated as
positive integers. More valuable stamps have a higher enumerated type. So, type 1 is the least valuable
type of stamps, type 2 is a bit more valuable, and so on.
Chef often communicates with other philatelists via a stamp exchange website called P-bay. On any
particular day, P-bay lists several offers, each of which is represented as an unordered pair {A, B}, allowing
its users to exchange stamps of type A with an equal number of stamps of type B. Chef can use such an
offer to put up any number of stamps of enumerated type A on the website and get the same number of
stamps of type B in return, or vice-versa (of course, he must have enough stamps of the corresponding
type). Assume that any number of stamps Chef wants are always available on the site's exchange market.
Each offer is open during only one day: Chef can't use it after this day, but he can use it several times
during this day. If there are some offers which are active during a given day, Chef can use them in any
order.
Chef is interested in making his collection as valuable as possible. Help him to find maximum possible
value of his collection after going through (accepting or declining) all the offers. Value of Chef's collection
is equal to the sum of type enumerations of all stamps in the collection.
Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test
cases follows.
The first line of each test case contains two integers N and M denoting number of stamps in Chef's
collection and number of offers, respectively.
Next line contains N integers denoting the enumerated types of stamps in Chef's collection.
The ith of the following M lines contains three integers Ti, Ai and Bi denoting that on day Ti, there is
an offer about exchanging stamps of types Ai and Bi.
Output

For each test case, output a single line containing maximum possible value of Chef's collection after all
offers.
Constraints

1 T 10
1 N 2*105
1 M 5*104
Types are enumerated with integers from 1 to 5*104
1 Ti 5*104
Subtasks

Subtask 1: (33 points)


1 M 1000
Types are enumerated with integers from 1 to 1000
1 Ti 1000
Subtask 2: (67 points)
Original constraints
Example

Input:

1
34
135
143
113
2 6 22
358

Output:

16

Explanation

Example case 1. Chef can use 2nd offer to exchange the stamp of type 1 with an stamp of type 3. After
that he can use 1st offer to exchange his 2 stamps of type 3 with 2 stamps of type 4. Finally, on 3rd day
Chef uses the offer to exchange the stamp of type 5 with an stamp of type 8. After this transaction, value
of his collection becomes 4 + 4 + 8 = 16.
solution to thisproblem..........................
#include <stdio.h>
#include <malloc.h>
#define MX 50002

int find_set(int E[],int x){


if(E[x]==x)return x;
E[x]=find_set(E,E[x]);
return E[x];
}

void set_union(int E[],int x,int y,int vertex[],int F[]){


int a=find_set(E,x),b=find_set(E,y);
if(F[vertex[a]]<F[vertex[b]])E[a]=E[b];
else E[b]=E[a];
}

void sort(int A[],int B[],int C[],int a,int n){


int i,D[MX],X[MX],Y[MX],Z[MX];
for(i=0;i<n;i++){
X[i]=A[i];
Y[i]=B[i];
Z[i]=C[i];
}
for(i=0;i<a+2;i++)D[i]=0;
for(i=0;i<n;i++)D[X[i]+1]++;

for(i=1;i<a+2;i++)D[i]+=D[i-1];
for(i=0;i<n;i++){
C[D[X[i]]]=Z[i];
B[D[X[i]]]=Y[i];
A[D[X[i]]]=X[i];
D[X[i]]++;
}
}

int main(){
int
J,i,j,maxab,maxd,temp,T,N,M,C[MX],D[MX],A[MX],B[MX],F[MX],E[MX],vertex[MX],inv_vertex[MX];
long long int ans;
for(i=0;i<MX;i++)vertex[i]=inv_vertex[i]=-1;
for(i=0;i<MX;i++)E[i]=F[i]=i;
for(i=0;i<MX;i++)C[i]=0;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
for(i=0,maxab=0;i<N;i++){
scanf("%d",&temp);
C[temp]++;
if(maxab<temp)maxab=temp;
}
for(i=0,maxd=0;i<M;i++){
scanf("%d%d%d",&D[i],&A[i],&B[i]);
if(maxd<D[i])maxd=D[i];
if(maxab<B[i])maxab=B[i];
if(maxab<A[i])maxab=A[i];
}
sort(D,B,A,maxd,M);
i=M-1;

while(i>=0){
j=0;
do{
if(inv_vertex[A[i]]==-1){
vertex[j]=A[i];
inv_vertex[A[i]]=j;
j++;
}
if(inv_vertex[B[i]]==-1){
vertex[j]=B[i];
inv_vertex[B[i]]=j;
j++;
}
set_union(E,inv_vertex[B[i]],inv_vertex[A[i]],vertex,F);
i--;
}while(D[i]==D[i+1]);
J=j;
for(j=0;j<J;j++){
temp=find_set(E,j);
if(F[vertex[temp]]>F[vertex[j]])F[vertex[j]]=F[vertex[temp]];
}
for(j=0;j<J;j++){
E[j]=j;
inv_vertex[vertex[j]]=-1;
vertex[j]=-1;
}
}
for(i=0,ans=0;i<maxab+1;i++)ans+=(long long int)((long long int)C[i]*(long long
int)F[i]);
printf("%lld\n",ans);
for(i=0;i<maxab+1;i++)F[i]=i;
for(i=0;i<maxab+1;i++)C[i]=0;

}
}

3 rd problem trip and pallindrome.............................


Serega is an avid traveller. He has already visited numerous exotic places, and now wants to embark on a
journey to Treeland. There are N cities in Treeland that Serega wants to visit. Also, roads have been laid
between some N-1 pairs of cities in Treeland. Roads are bidirectional, and there exists a unique path
between any two Treeland cities.
Each of Treeland's roads has its own class, wich is specified using a single digit. Serega's journey is a path
between some cities, i.e. he never visits one city more than once. But Serega wants to select a path such
that the sequence of classes on the path is a palindrome. Of course, Serega wants to visit as many cities
as possible. Please help Him.
Input

The first line of input contains a single integer T denoting the number of test cases. The description of T
test cases follows.
The first line of each case contains a single integer N. The next N-1 lines contain the roads. Each line
contains three integers v, u and c, meaning that there is a road of class c between the vth and uth
(assuming 1-indexing) cities of Treeland.
Output

For each test case, you should output the maximum number of cities that Serega can visit.
Constraints

1 T 3*105
1 N 105
Sum of N over all test cases in a single file will not be greater then 3*105
0c9
1 u, v N
The roads form a tree.
Subtasks

Subtask #1 (15 points): 1 N 102


Subtask #2: (85 points): 1 N 105
Example

Input:
1
8
121
130
341
452
160
671
782

Output:
7
Explanation

Sequence of classes on the path between the cities 5 and 8 is 210012.


solution to trip and pallindrome ............................
#include <stdio.h>
#include <malloc.h>
#define MX 100005

typedef struct _node{


int vx,cost;
struct _node *next;
}node;

typedef node * pnode;

void insert(pnode *head,int v,int c){


pnode p=(pnode)malloc(sizeof(node));
p->vx=v;

p->cost=c;
p->next=*head;
*head=p;
}

int calculate(int p[],int n){


int count,i,j;
if(n==0)return 0;
for(i=0,count=1;i+1<n;i++){
if(p[i]==p[i+1]){
j=1;
while(p[i-j]==p[i+j+1]&&i-j>=0&&i+j+1<n)j++;
if(count<2*j)count=2*j;
}
}
for(i=0;i+2<n;i++){
if(p[i]==p[i+2]){
j=1;
while(p[i-j]==p[i+j+2]&&i-j>=0&&i+j+2<n)j++;
if(count<2*j+1)count=2*j+1;
}
}
return count;
}

void traverse(pnode AL[],int index,int from,int palindrome[],int size,int *max){


int count;
if(AL[index]->next==NULL&&from!=-1){
count=calculate(palindrome,size);
if(count>*max)*max=count;
}

pnode p=AL[index];
for(p=AL[index];p!=NULL;p=p->next){
if(p->vx==from)continue;
palindrome[size]=p->cost;
traverse(AL,p->vx,index,palindrome,size+1,max);
}
}

int main(){
int i,u,v,c,T,N,max,palindrome[MX+1];
pnode AL[MX+1];
scanf("%d",&T);
while(T--){
scanf("%d",&N);
for(i=0;i<=N;i++)AL[i]=NULL;
for(i=0;i<N-1;i++){
scanf("%d%d%d",&u,&v,&c);
insert(&AL[u],v,c);
insert(&AL[v],u,c);
}
for(i=1,max=0;i<=N;i++)if(AL[i]!=NULL&&AL[i]->next==NULL)traverse(AL,i,1,palindrome,0,&max);
printf("%d\n",max+1);
}

Simple Sum
given a positive integer n, Chef has asked you to calculate the following sum in the most efficient way:

Here gcd(a,b) means greatest common divisor of two integers a and b.


Input

The first line of input contains an integer T denoting the number of test cases. The description of T test

cases follows.The only line describing each test case contains a single integer n, given to you by Chef.

Please note that input can be very large. So it's recommended to use fast IO methods.
Output

For each test case, output a single line containing one integer: answer to Chef's query.
Constraints and Subtasks

1 T 106
1 n 107

Subtask1(10 points): 1 sum of n over all testcases 107, TL=1.5s


Subtask2(20 points): 1 n 104,
TL=1s
Subtask3(70 points): No additional constraints,
TL=1s
Example

Input:
5
1
2
3
4
5

Output:
1
3
7

11
21

Das könnte Ihnen auch gefallen