Wednesday, April 2, 2014

codechef PAIRING - Pairing Chefs solution

codechef PAIRING - Pairing Chefs: http://www.codechef.com/problems/PAIRING

codechef PAIRING - Pairing Chefs editorial: http://discuss.codechef.com/questions/4192/pairing-editorial

codechef PAIRING - Pairing Chefs solution: http://ideone.com/9RxRic


#include <iostream>
#include <cstdio>
using namespace std;

int main() {
int t, n, m, a[10005], b[10005], c[1005], d[10005];
scanf("%d", &t);
while(t--) {
for(int i=0; i<10005; i++) a[i]=b[i]=d[i]=0;
for(int i=0; i<1005; i++) c[i]=0;
scanf("%d%d", &n, &m);
for(int i=0; i<m; i++) scanf("%d%d", &a[i], &b[i]);
for(int i=m-1; i>=0; i--) if(c[a[i]]==0 && c[b[i]]==0) d[i]++, c[a[i]]++, c[b[i]]++;
for(int i=0; i<m; i++) if(d[i]) printf("%d ", i);
printf("\n");
}
return 0;
}

note: it is already obivous that chef will start from the highest i'th pair to keep food at its best quality(quality of food=total pow(2, i)). so start from last (m-1'th) index, that index certainly will be on pairing list of chef(d[i]). go on till 1st(0'th)  index of pairings, meanwhile on each check whether any of those potential pairs (u, v) have been used before, if not(if(c[a[i]]==0 && c[b[i]]==0)), then add those pairs on the chef's list(d[i]++), and mark those pairs as used(c[a[i]]++, c[b[i]]++). at the end, starting from beginning to the end(for(int i=0; i<m; i++) check whether that index of pairs included on the chef's list (if(d[i])), if yes, then print that index.




No comments:

Post a Comment