Hi. I am getting WA for CODESPTE. Here is the link to the problem. I got AC for the exact same problem at HackerRank (link) and used the same code for this problem in SPOJ. However, I got WA. I feel that something might be wrong with the test data.
Here is my code:
// Li Hong Sheng Gabriel's Competitive Programming Template v2017.7
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tag_and_trait.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define em emplace
#define len(s) (int) s.length()
#define sz(v) (int) v.size()
#define p32 pair<int,int>
#define p64 pair<ll,ll>
#define pdd pair<double,double>
#define fi first
#define se second
#define repn(i,e) for(int i = 0; i < e; i++)
#define repsn(i,s,e) for(int i = s; i < e; i++)
#define rrepn(i,s) for(int i = s; i >= 0; i--)
#define rrepsn(i,s,e) for(int i = s; i >= e; i--)
#define v64 vector<ll>
#define v32 vector<int>
#define vp64 vector<p64>
#define vp32 vector<p32>
#define aprint(a,s) if(DRAFT) { cout << "DEBUG: "; repn(k,s) { cout << setw(4) << right << a[k] << " "; } cout << endl; }
#define mprint(aa,rn,cn) if(DRAFT) { repn(i,rn) { aprint(aa[i],cn); } cout << endl; }
#define vprint(a) if(DRAFT) { cout << "DEBUG: "; repn(k,sz(a)) { cout << setw(4) << right << a[k] << " "; } cout << endl; }
#define vrprint(a,b) if(DRAFT) { cout << "DEBUG: "; repn(k,b) { cout << setw(4) << right << a[k] << " "; } cout << endl; }
#define vvprint(a) if(DRAFT) repn(i,sz(a)) { cout << "DEBUG: " << i << ": "; repn(j,sz(a[i])) { cout << setw(4) << right << a[i][j] << " "; } cout << endl; }
#define vvrprint(a,b) if(DRAFT) repn(i,b) { cout << "DEBUG: " << i << ": "; repn(j,sz(a[i])) { cout << setw(4) << right << a[i][j] << " "; } cout << endl; }
#define value(a) if(DRAFT) { cout << "DEBUG: The value of " << #a << " is " << a << endl; }
#define valuep(p) { if(DRAFT) cout << "DEBUG: " << "( " << p.fi << ", " << p.se << " )" << endl; }
#define message(s) { if(DRAFT) cout << "MESSAGE: " << s << endl; }
#define inf32 INT_MAX
#define inf64 LLONG_MAX
#define all(a) a.begin(),a.end()
#define UM unordered_map
#define US unordered_set
#define EPS 1e-10
#define breakl "\n"
#define MOD 1000000007
// Uncomment the include files, namespace and type to use
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> AVL;
// Usage for AVL:
// set functions, rank and select:
//select: cout<<*X.find_by_order(>=1)<<endl;
//rank: cout<<X.order_of_key(>=1)<<endl;
inline void scale(int x) { cout.precision(x); cout << fixed; }
double dist(pdd x,pdd y) { return sqrt((x.fi-y.fi)*(x.fi-y.fi)+(x.se-y.se)*(x.se-y.se)); }
ll lpow(ll x, int y) { if(!y)return 1;ll t=lpow(x,y/2);if(y&1)return(y>0)?x*t*t:(t*t)/x;return t*t; }
int ipow(int x, int y) { if(!y)return 1;int t=ipow(x,y/2);if(y&1)return(y>0)?x*t*t:(t*t)/x;return t*t; }
void split(vector<string> &tmp,string &line,char * buffer,char delim=' ') {
strcpy(buffer,line.c_str());
char * token = strtok(buffer,&delim);
while(token != 0) {
tmp.eb(string(token));
token = strtok(NULL,&delim);
}
}
inline void fastio(int debug) {
if(debug) {
cout << "DEBUGGING MODE..." << endl;
freopen("in","r",stdin);
} else {
ios_base::sync_with_stdio(false), cin.tie(0);
}
}
// End of Template
bool DRAFT = 1; // DO NOT REMOVE THIS LINE
#define MAX_N 10000
int n,t,a,b;
v32 graph[MAX_N];
int fact[MAX_N];
void factMod(int a, int MODULO) {
int cnt = 1;
fact[0] = 1;
while (cnt <= a) fact[cnt] = (int)((long long) fact[cnt-1] * cnt % MODULO), cnt++;
}
int solve() {
int cen = n; int ans = 1;
repn(u,n) {
if(sz(graph[u]) == 1) {
cen--;
continue;
}
int small = 0, big = 0;
for(auto v : graph[u]) {
if(sz(graph[v]) > 1) big++;
else small++;
if(big > 2) return 0;
}
ans = 1LL * ans * fact[small] % MOD;
}
return cen == 1 ? ans : (ans * 2LL % MOD);
}
int main(void) {
fastio(0);
factMod(MAX_N-1,MOD);
cin >> t;
while(t--) {
cin >> n;
fill(graph,graph+n,v32());
repn(i,n-1) {
cin >> a >> b;
graph[a].eb(b);
graph[b].eb(a);
}
cout << solve() << breakl;
}
return 0;
}
Is someone able to assist me on this?