一門武功能否傳承久遠並被髮揚光大,是要看緣分的。一般來說,師傅傳授給徒弟的武功總要打個折扣,於是越往後傳,弟子們的功夫就越弱…… 直到某一支的某一代突然出現一個天分特別高的弟子(或者是喫到了靈丹、挖到了特別的祕笈),會將功夫的威力一下子放大N倍 —— 我們稱這種弟子爲“得道者”。
這裏我們來考察某一位祖師爺門下的徒子徒孫家譜:假設家譜中的每個人只有1位師傅(除了祖師爺沒有師傅);每位師傅可以帶很多徒弟;並且假設輩分嚴格有序,即祖師爺這門武功的每個第i
代傳人只能在第i-1
代傳人中拜1個師傅。我們假設已知祖師爺的功力值爲Z
,每向下傳承一代,就會減弱r%
,除非某一代弟子得道。現給出師門譜系關係,要求你算出所有得道者的功力總值。
輸入格式:
輸入在第一行給出3個正整數,分別是:NNN(≤105\le 10^5≤105)——整個師門的總人數(於是每個人從0到N−1N-1N−1編號,祖師爺的編號爲0);ZZZ——祖師爺的功力值(不一定是整數,但起碼是正數);rrr ——每傳一代功夫所打的折扣百分比值(不超過100的正數)。接下來有NNN行,第iii行(i=0,⋯,N−1i=0, \cdots , N-1i=0,⋯,N−1)描述編號爲iii的人所傳的徒弟,格式爲:
KiK_iKi ID[1] ID[2] ⋯\cdots⋯ ID[KiK_iKi]
其中KiK_iKi是徒弟的個數,後面跟的是各位徒弟的編號,數字間以空格間隔。KiK_iKi爲零表示這是一位得道者,這時後面跟的一個數字表示其武功被放大的倍數。
輸出格式:
在一行中輸出所有得道者的功力總值,只保留其整數部分。題目保證輸入和正確的輸出都不超過101010^{10}1010。
輸入樣例:
10 18.0 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3
輸出樣例:
404
dfs,7個測試點4個超時
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;
//** Class for buffered reading int and double values *//*
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
// ** call this method to initialize reader for InputStream *//*
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
// ** get next word *//*
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static boolean hasNext()throws IOException {
return tokenizer.hasMoreTokens();
}
static String nextLine() throws IOException{
return reader.readLine();
}
static char nextChar() throws IOException{
return next().charAt(0);
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static float nextFloat() throws IOException {
return Float.parseFloat(next());
}
}
public class Main {
static int n;
static float z;
static float r;
static Map<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>(); //鍵是父親編號,值是兒子列表
static int[]tag = new int[100000];//得到者倍數
static float sum;
public static void main(String[] args) throws IOException {
Reader.init(System.in);
n = Reader.nextInt();
z = Reader.nextFloat();
r = Reader.nextFloat();
for (int i = 0; i < tag.length; i++) {
tag[i] = -1; //測試點有得到提升0倍的...
}
for (int i = 0; i < n; i++) {
int k = Reader.nextInt();
if (k==0) {
tag[i] = Reader.nextInt();
} else {
map.put(i, new ArrayList<Integer>());
for (int j = 0; j < k; j++) {
map.get(i).add(Reader.nextInt());
}
}
}
dfs(0,z);
System.out.println((int)sum);
}
private static void dfs(int i, float z) {
if (tag[i]!=-1) {
sum+=tag[i]*z;
} else {
for (int j = 0; j < map.get(i).size(); j++) {
dfs(map.get(i).get(j), z*(100-r)/100);
}
}
}
}
層次遍歷,結果也是4個超時。。。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;
//** Class for buffered reading int and double values *//*
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
// ** call this method to initialize reader for InputStream *//*
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
// ** get next word *//*
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static boolean hasNext()throws IOException {
return tokenizer.hasMoreTokens();
}
static String nextLine() throws IOException{
return reader.readLine();
}
static char nextChar() throws IOException{
return next().charAt(0);
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static float nextFloat() throws IOException {
return Float.parseFloat(next());
}
}
public class Main {
static int n;
static float z;
static float r;
static class Person{
float z;
ArrayList<Integer>v;
int bei;
int tag;
}
static float sum;
public static void main(String[] args) throws IOException {
Reader.init(System.in);
n = Reader.nextInt();
z = Reader.nextFloat();
r = Reader.nextFloat();
Person[]person = new Person[n];
for (int i = 0; i < person.length; i++) {
person[i] = new Person();
person[i].v = new ArrayList<Integer>();
}
for (int i = 0; i < n; i++) {
int k = Reader.nextInt();
if (k==0) {
person[i].tag = 1;
person[i].bei = Reader.nextInt();
} else {
for (int j = 0; j < k; j++) {
person[i].v.add(Reader.nextInt());
}
}
}
Queue<Integer>q = new LinkedList<Integer>();
person[0].z = z;
q.offer(0);
while (!q.isEmpty()) {
int tmp = q.poll();
if (person[tmp].tag==1) {
person[tmp].z = person[tmp].z*person[tmp].bei;
sum+=person[tmp].z;
}
else {
for (int i = 0; i < person[tmp].v.size(); i++) {
q.offer(person[tmp].v.get(i));
person[person[tmp].v.get(i)].z = person[tmp].z*(100-r)/100;
}
}
}
System.out.println((int)sum);
}
}
並查集,2個點超時
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int N = Reader.nextInt();
player = new int[N];
Z = Reader.nextDouble();
r = (double) (1 - 0.01 * Reader.nextDouble());
double sum = 0;
int num = 0;
int[][] dedao = new int[N][2];
for (int i = 0; i < player.length; i++) {
int n = Reader.nextInt();
if (n == 0) {
// 得道者
dedao[num][0] = i;
dedao[num++][1] = Reader.nextInt();
} else {
for (int j = 0; j < n; j++) {
player[Reader.nextInt()] = i;
}
}
}
for (int i = 0; i < num; i++) {
sum+=get(dedao[i][0],dedao[i][1]);
}
System.out.printf("%d", (int) sum);
}
static double Z, r;
static int[] player;
static double get(int id, int factor) {
int count = 0;
int current = id;
while (player[current] != current) {
current = player[current];
count++;
}
return (double) (Z * Math.pow(r, count) * factor);
}
}
// Class for buffered reading int and double values *//*
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
// ** call this method to initialize reader for InputStream *//*
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static void init(File file) {
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tokenizer = new StringTokenizer("");
}
// ** get next word *//*
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static String nextLine() throws IOException {
return reader.readLine();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static char nextChar() throws IOException {
return next().toCharArray()[0];
}
static float nextFloat() throws IOException {
return Float.parseFloat(next());
}
static double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(next());
}
}
============================================================================
又寫了一遍,一個點超時
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import javax.management.monitor.Monitor;
import javax.security.auth.callback.LanguageCallback;
//** Class for buffered reading int and double values *//*
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
// ** call this method to initialize reader for InputStream *//*
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
// ** get next word *//*
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add ceck for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static boolean hasNext()throws IOException {
return tokenizer.hasMoreTokens();
}
static String nextLine() throws IOException{
return reader.readLine();
}
static char nextChar() throws IOException{
return next().charAt(0);
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static float nextFloat() throws IOException {
return Float.parseFloat(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static void close() throws IOException {
reader.close();
}
}
class Writer{
static BufferedWriter writer;
static void init(OutputStream outputStream) {
writer = new BufferedWriter(new OutputStreamWriter(outputStream));
}
static void print(Object object) throws IOException {
writer.write(object.toString());
}
static void println(Object object) throws IOException {
writer.write(object.toString());
writer.write("\n");
}
static void close() throws IOException {
// TODO Auto-generated method stub
writer.close();
}
}
public class Main {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
Writer.init(System.out);
solve();
Reader.close();
Writer.close();
}
static int[]pre;
private static void solve() throws IOException {
int n = Reader.nextInt();
pre = new int[n+1];
for (int i = 0; i < pre.length; i++) {
pre[i] = i;
}
double z =Reader.nextDouble();
double r =(double)(1-0.01*Reader.nextDouble());
double sum = 0;
for (int i = 0; i < n; i++) {
int k = Reader.nextInt();
if (k==0) {
int beishu = Reader.nextInt();
int daishu = find(i);
double t = z;
t = t*Math.pow(r, daishu);
sum+=t*beishu;
}else {
for (int j = 0; j < k; j++) {
int id = Reader.nextInt();
pre[id] = i;
}
}
}
Writer.println((int)sum);
}
static int find(int id) {
int daishu = 0;
while (id!=pre[id]) {
id = pre[id];
daishu++;
}
return daishu;
}
}